Skip to content

Commit

Permalink
improve doc
Browse files Browse the repository at this point in the history
added example to color all errors in red
  • Loading branch information
lmittmann committed Sep 10, 2024
1 parent 368de75 commit c67ac01
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
20 changes: 20 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand Down

0 comments on commit c67ac01

Please sign in to comment.