From c67ac01da4038709977d7b60c695966e77ec2498 Mon Sep 17 00:00:00 2001 From: lmittmann Date: Tue, 10 Sep 2024 16:08:37 +0200 Subject: [PATCH] improve doc added example to color all errors in red --- README.md | 19 +++++++++++++++++++ handler.go | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/README.md b/README.md index 88a77ac..ef03755 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,25 @@ logger := slog.New( ) ``` +```go +// create a new logger that writes all errors in red +w := os.Stderr +logger := slog.New( + tint.NewHandler(w, &tint.Options{ + ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { + err, ok := a.Value.Any().(error) + if !ok { + return a + } + + aErr = tint.Err(err) + aErr.Key = a.Key + return aErr + }, + }), +) +``` + ### Automatically Enable Colors Colors are enabled by default and can be disabled using the `Options.NoColor` diff --git a/handler.go b/handler.go index d54b5d7..bc077f4 100644 --- a/handler.go +++ b/handler.go @@ -12,6 +12,8 @@ Options.ReplaceAttr can be used to alter or drop attributes. If set, it is called on each non-group attribute before it is logged. See [slog.HandlerOptions] for details. +Create a new logger that doesn't write the time: + w := os.Stderr logger := slog.New( tint.NewHandler(w, &tint.Options{ @@ -24,6 +26,24 @@ See [slog.HandlerOptions] for details. }), ) +Create a new logger that writes all errors in red: + + w := os.Stderr + logger := slog.New( + tint.NewHandler(w, &tint.Options{ + ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { + err, ok := a.Value.Any().(error) + if !ok { + return a + } + + aErr = tint.Err(err) + aErr.Key = a.Key + return aErr + }, + }), + ) + # Automatically Enable Colors Colors are enabled by default and can be disabled using the Options.NoColor