🧻 devslog - Go slog.Handler for development
devslog
is a zero dependency structured logging handler for Go's log/slog
package with pretty and colorful output for developers.
go get github.com/golang-cz/devslog@latest
logger := slog.New(devslog.NewHandler(os.Stdout, nil))
// optional: set global logger
slog.SetDefault(logger)
// new logger with options
opts := &devslog.Options{
MaxSlicePrintSize: 4,
SortKeys: true,
TimeFormat: "[04:05]",
NewLineAfterLog: true,
DebugColor: devslog.Magenta,
StringerFormatter: true,
}
logger := slog.New(devslog.NewHandler(os.Stdout, opts))
// optional: set global logger
slog.SetDefault(logger)
Handler accepts default slog.HandlerOptions
// slog.HandlerOptions
slogOpts := &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
}
// new logger with options
opts := &devslog.Options{
HandlerOptions: slogOpts,
MaxSlicePrintSize: 4,
SortKeys: true,
NewLineAfterLog: true,
StringerFormatter: true,
}
logger := slog.New(devslog.NewHandler(os.Stdout, opts))
// optional: set global logger
slog.SetDefault(logger)
slogOpts := &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
}
var logger *slog.Logger
if production {
logger = slog.New(slog.NewJSONHandler(os.Stdout, slogOpts))
} else {
opts := &devslog.Options{
HandlerOptions: slogOpts,
MaxSlicePrintSize: 10,
SortKeys: true,
NewLineAfterLog: true,
StringerFormatter: true,
}
logger = slog.New(devslog.NewHandler(os.Stdout, opts))
}
// optional: set global logger
slog.SetDefault(logger)
Parameter | Description | Default | Value |
---|---|---|---|
MaxSlicePrintSize | Specifies the maximum number of elements to print for a slice. | 50 | uint |
SortKeys | Determines if attributes should be sorted by keys. | false | bool |
TimeFormat | Time format for timestamp. | "[15:04:05]" | string |
NewLineAfterLog | Add blank line after each log | false | bool |
StringIndentation | Indent \n in strings | false | bool |
DebugColor | Color for Debug level | devslog.Blue | devslog.Color (uint) |
InfoColor | Color for Info level | devslog.Green | devslog.Color (uint) |
WarnColor | Color for Warn level | devslog.Yellow | devslog.Color (uint) |
ErrorColor | Color for Error level | devslog.Red | devslog.Color (uint) |
MaxErrorStackTrace | Max stack trace frames for errors | 0 | uint |
StringerFormatter | Use Stringer interface for formatting | false | bool |
NoColor | Disable coloring | false | bool |