Documentation ¶
Overview ¶
Package slogutil contains extensions and utilities for package log/slog from the standard library.
Index ¶
- Constants
- func New(c *Config) (l *slog.Logger)
- func NewDiscardLogger() (l *slog.Logger)
- func PrintStack(ctx context.Context, l *slog.Logger, lvl slog.Level)
- func RecoverAndLog(ctx context.Context, l *slog.Logger)
- func RemoveTime(groups []string, a slog.Attr) (res slog.Attr)
- type BadFormatError
- type Config
- type DiscardHandler
- type Format
- type LevelHandler
- func (h *LevelHandler) Enabled(_ context.Context, level slog.Level) (ok bool)
- func (h *LevelHandler) Handle(ctx context.Context, r slog.Record) (err error)
- func (h *LevelHandler) Handler() (unwrapped slog.Handler)
- func (h *LevelHandler) WithAttrs(attrs []slog.Attr) (res slog.Handler)
- func (h *LevelHandler) WithGroup(name string) (res slog.Handler)
Examples ¶
Constants ¶
const ( FormatDefault = "default" FormatJSON = "json" FormatText = "text" )
Valid formats.
const ( KeyPrefix = "prefix" KeyError = "err" )
Additional key constants.
Variables ¶
This section is empty.
Functions ¶
func New ¶
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 ¶
NewDiscardLogger returns a new logger that uses DiscardHandler.
func PrintStack ¶
PrintStack logs the stacktrace into l on the given level.
func RecoverAndLog ¶
RecoverAndLog recovers from a panic and logs the panic value into l along with the stacktrace.
func RemoveTime ¶
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 ¶
Enabled implements the slog.Handler interface for DiscardHandler. It always returns false.
func (DiscardHandler) Handle ¶
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 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 ¶
Enabled implements the slog.Handler interface for *LevelHandler. It reports whether level is as high as h's level.
func (*LevelHandler) Handle ¶
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.