Documentation
¶
Overview ¶
Package tint provides a slog.Handler that writes tinted logs. The output format is inspired by the zerolog.ConsoleWriter.
The output format can be customized using Options, which is a drop-in replacement for slog.HandlerOptions.
Example ¶
package main import ( "errors" "os" "time" "github.com/fgimian/tint" "golang.org/x/exp/slog" ) func main() { slog.SetDefault(slog.New(tint.Options{ Level: slog.LevelDebug, TimeFormat: time.Kitchen, }.NewHandler(os.Stderr))) slog.Info("Starting server", "addr", ":8080", "env", "production") slog.Debug("Connected to DB", "db", "myapp", "host", "localhost:5432") slog.Warn("Slow request", "method", "GET", "path", "/users", "duration", 497*time.Millisecond) slog.Error("DB connection lost", tint.Err(errors.New("connection reset")), "db", "myapp") }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewHandler ¶
NewHandler creates a slog.Handler that writes tinted logs to Writer w, using the default options.
Types ¶
type Options ¶
type Options struct { // Enable source code location (Default: false) AddSource bool // Minimum level to log (Default: slog.LevelInfo) Level slog.Leveler // ReplaceAttr is called to rewrite each non-group attribute before it is logged. // See https://pkgo.dev/golang.org/x/exp/slog#HandlerOptions for details. ReplaceAttr func(groups []string, attr slog.Attr) slog.Attr // Time format (Default: time.StampMilli) TimeFormat string // Disable color (Default: false) NoColor bool }
Options for a slog.Handler that writes tinted logs. A zero Options consists entirely of default values.
Options can be used as a drop-in replacement for slog.HandlerOptions.
func (Options) NewHandler ¶
NewHandler creates a slog.Handler that writes tinted logs to Writer w with the given options.
Click to show internal directories.
Click to hide internal directories.