Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lmittmann/tint
🌈 slog.Handler that writes tinted (colorized) logs
https://github.com/lmittmann/tint
ansi color golang logging slog
Last synced: 9 days ago
JSON representation
🌈 slog.Handler that writes tinted (colorized) logs
- Host: GitHub
- URL: https://github.com/lmittmann/tint
- Owner: lmittmann
- License: mit
- Created: 2023-03-19T21:02:26.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-10T14:15:01.000Z (about 2 months ago)
- Last Synced: 2024-10-13T19:12:18.564Z (26 days ago)
- Topics: ansi, color, golang, logging, slog
- Language: Go
- Homepage: https://pkg.go.dev/github.com/lmittmann/tint
- Size: 43.9 KB
- Stars: 737
- Watchers: 7
- Forks: 38
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - tint - A slog.Handler that writes tinted logs. (Logging / Search and Analytic Databases)
- awesome-ccamel - lmittmann/tint - 🌈 slog.Handler that writes tinted (colorized) logs (Go)
- awesome-slog - tint
README
# `tint`: 🌈 **slog.Handler** that writes tinted logs
[![Go Reference](https://pkg.go.dev/badge/github.com/lmittmann/tint.svg)](https://pkg.go.dev/github.com/lmittmann/tint#section-documentation)
[![Go Report Card](https://goreportcard.com/badge/github.com/lmittmann/tint)](https://goreportcard.com/report/github.com/lmittmann/tint)
Package `tint` implements a zero-dependency [`slog.Handler`](https://pkg.go.dev/log/slog#Handler)
that writes tinted (colorized) logs. Its output format is inspired by the `zerolog.ConsoleWriter` and
[`slog.TextHandler`](https://pkg.go.dev/log/slog#TextHandler).The output format can be customized using [`Options`](https://pkg.go.dev/github.com/lmittmann/tint#Options)
which is a drop-in replacement for [`slog.HandlerOptions`](https://pkg.go.dev/log/slog#HandlerOptions).```
go get github.com/lmittmann/tint
```## Usage
```go
w := os.Stderr// create a new logger
logger := slog.New(tint.NewHandler(w, nil))// set global logger with custom options
slog.SetDefault(slog.New(
tint.NewHandler(w, &tint.Options{
Level: slog.LevelDebug,
TimeFormat: time.Kitchen,
}),
))
```### Customize Attributes
`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`](https://pkg.go.dev/log/slog#HandlerOptions)
for details.```go
// create a new logger that doesn't write the time
w := os.Stderr
logger := slog.New(
tint.NewHandler(w, &tint.Options{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey && len(groups) == 0 {
return slog.Attr{}
}
return a
},
}),
)
``````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 {
if err, ok := a.Value.Any().(error); ok {
aErr := tint.Err(err)
aErr.Key = a.Key
return aErr
}
return a
},
}),
)
```### Automatically Enable Colors
Colors are enabled by default and can be disabled using the `Options.NoColor`
attribute. To automatically enable colors based on the terminal capabilities,
use e.g. the [`go-isatty`](https://github.com/mattn/go-isatty) package.```go
w := os.Stderr
logger := slog.New(
tint.NewHandler(w, &tint.Options{
NoColor: !isatty.IsTerminal(w.Fd()),
}),
)
```### Windows Support
Color support on Windows can be added by using e.g. the
[`go-colorable`](https://github.com/mattn/go-colorable) package.```go
w := os.Stderr
logger := slog.New(
tint.NewHandler(colorable.NewColorable(w), nil),
)
```