slogutil

package
v0.0.0-...-9d39026 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2024 License: Unlicense Imports: 7 Imported by: 0

Documentation

Overview

Package slogutil contains extensions and utilities for package log/slog from the standard library.

Index

Examples

Constants

View Source
const (
	FormatDefault = "default"
	FormatJSON    = "json"
	FormatText    = "text"
)

Valid formats.

View Source
const (
	KeyPrefix = "prefix"
	KeyError  = "err"
)

Additional key constants.

Variables

This section is empty.

Functions

func New

func New(c *Config) (l *slog.Logger)

New creates a slog logger with the given parameters. c must not be nil and its fields must be valid.

Example (Default)
package main

import (
	"os"

	"github.com/Potterli20/golibs-fork/logutil/slogutil"
)

func main() {
	l := slogutil.New(&slogutil.Config{
		Output:       os.Stdout,
		Format:       slogutil.FormatDefault,
		AddTimestamp: false,
		Verbose:      true,
	})

	l.Info("test info")
	l.Debug("test debug")

}
Output:

INFO test info
DEBUG test debug
Example (Json)
package main

import (
	"os"

	"github.com/Potterli20/golibs-fork/logutil/slogutil"
)

func main() {
	l := slogutil.New(&slogutil.Config{
		Output:       os.Stdout,
		Format:       slogutil.FormatJSON,
		AddTimestamp: false,
		Verbose:      true,
	})

	l.Info("test info")
	l.Debug("test debug")

	l.WithGroup("test_group").Info("group test info", "time", "too late")
	l.WithGroup("test_group").Debug("group test debug", "time", "too late")

}
Output:

{"level":"INFO","msg":"test info"}
{"level":"DEBUG","msg":"test debug"}
{"level":"INFO","msg":"group test info","test_group":{"time":"too late"}}
{"level":"DEBUG","msg":"group test debug","test_group":{"time":"too late"}}
Example (Text)
package main

import (
	"os"

	"github.com/Potterli20/golibs-fork/logutil/slogutil"
)

func main() {
	l := slogutil.New(&slogutil.Config{
		Output:       os.Stdout,
		Format:       slogutil.FormatText,
		AddTimestamp: false,
		Verbose:      true,
	})

	l.Info("test info")
	l.Debug("test debug")

	l.WithGroup("test_group").Info("group test info", "time", "too late")
	l.WithGroup("test_group").Debug("group test debug", "time", "too late")

}
Output:

level=INFO msg="test info"
level=DEBUG msg="test debug"
level=INFO msg="group test info" test_group.time="too late"
level=DEBUG msg="group test debug" test_group.time="too late"

func NewDiscardLogger

func NewDiscardLogger() (l *slog.Logger)

NewDiscardLogger returns a new logger that uses DiscardHandler.

func PrintStack

func PrintStack(ctx context.Context, l *slog.Logger, lvl slog.Level)

PrintStack logs the stacktrace into l on the given level.

func RecoverAndLog

func RecoverAndLog(ctx context.Context, l *slog.Logger)

RecoverAndLog recovers from a panic and logs the panic value into l along with the stacktrace.

func RemoveTime

func RemoveTime(groups []string, a slog.Attr) (res slog.Attr)

RemoveTime is a function for slog.HandlerOptions.ReplaceAttr that removes the "time" attribute.

Types

type BadFormatError

type BadFormatError struct {
	Format string
}

BadFormatError is an error about a bad logging format.

func (*BadFormatError) Error

func (err *BadFormatError) Error() (msg string)

Error implements the [error] interface for *BadFormatError.

type Config

type Config struct {
	// Output is the output destination.  It must not be nil.
	Output io.Writer

	// Format is the format for the logs.  It must be valid.
	Format Format

	// AddTimestamp, if true, adds a timestamp to every record.
	AddTimestamp bool

	// Verbose, if true, enables verbose logging.
	Verbose bool
}

Config contains the configuration for a logger.

type DiscardHandler

type DiscardHandler struct{}

DiscardHandler ignores all messages.

func (DiscardHandler) Enabled

func (h DiscardHandler) Enabled(_ context.Context, _ slog.Level) (ok bool)

Enabled implements the slog.Handler interface for DiscardHandler. It always returns false.

func (DiscardHandler) Handle

func (h DiscardHandler) Handle(_ context.Context, _ slog.Record) (err error)

Handle implements the slog.Handler interface for DiscardHandler. It always returns nil.

func (DiscardHandler) WithAttrs

func (h DiscardHandler) WithAttrs(_ []slog.Attr) (res slog.Handler)

WithAttrs implements the slog.Handler interface for DiscardHandler. It always returns h.

func (DiscardHandler) WithGroup

func (h DiscardHandler) WithGroup(_ string) (res slog.Handler)

WithGroup implements the slog.Handler interface for DiscardHandler. It always returns h.

type Format

type Format string

Format represents an acceptable format of logs.

func NewFormat

func NewFormat(s string) (f Format, err error)

NewFormat returns a new valid format.

type LevelHandler

type LevelHandler struct {
	// contains filtered or unexported fields
}

A LevelHandler wraps a Handler with an Enabled method that returns false for levels below a minimum.

See https://cs.opensource.google/go/x/exp/+/master:slog/example_level_handler_test.go.

func NewLevelHandler

func NewLevelHandler(level slog.Leveler, h slog.Handler) (lh *LevelHandler)

NewLevelHandler returns a LevelHandler with the given level. All methods except Enabled delegate to h.

func (*LevelHandler) Enabled

func (h *LevelHandler) Enabled(_ context.Context, level slog.Level) (ok bool)

Enabled implements the slog.Handler interface for *LevelHandler. It reports whether level is as high as h's level.

func (*LevelHandler) Handle

func (h *LevelHandler) Handle(ctx context.Context, r slog.Record) (err error)

Handle implements the slog.Handler interface for *LevelHandler.

func (*LevelHandler) Handler

func (h *LevelHandler) Handler() (unwrapped slog.Handler)

Handler returns the slog.Handler wrapped by h.

func (*LevelHandler) WithAttrs

func (h *LevelHandler) WithAttrs(attrs []slog.Attr) (res slog.Handler)

WithAttrs implements the slog.Handler interface for *LevelHandler.

func (*LevelHandler) WithGroup

func (h *LevelHandler) WithGroup(name string) (res slog.Handler)

WithGroup implements the slog.Handler interface for *LevelHandler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL