Skip to content
/ tint Public

๐ŸŒˆ slog.Handler that writes tinted (colorized) logs

License

Notifications You must be signed in to change notification settings

lmittmann/tint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

16 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

tint: ๐ŸŒˆ slog.Handler that writes tinted logs

Go Reference Go Report Card



Package tint provides a slog.Handler that writes tinted logs. The output format is inspired by the zerolog.ConsoleWriter.

go get github.com/lmittmann/tint

Note

slog is an experimental structured logging package, that will be added to the standard library in Go 1.21. See #56345 for tracking the progress.

Usage

// create a new logger
logger := slog.New(tint.NewHandler(os.Stderr))

// set global logger with custom options
slog.SetDefault(slog.New(tint.Options{
	Level:      slog.LevelDebug,
	TimeFormat: time.Kitchen,
}.NewHandler(os.Stderr)))

Customize

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
logger := slog.New(tint.Options{
	ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
		if a.Key == slog.TimeKey && len(groups) == 0 {
			a.Key = ""
		}
		return a
	},
}.NewHandler(os.Stderr))

Windows

ANSI color support in the terminal on Windows can be enabled by using e.g. go-colorable.

logger := slog.New(tint.NewHandler(colorable.NewColorableStderr()))