Documentation
¶
Overview ¶
Package tint implements a zero-dependency slog.Handler that writes tinted (colorized) logs. The output format is inspired by the zerolog.ConsoleWriter and slog.TextHandler.
The output format can be customized using TintOptions, which is a drop-in replacement for slog.HandlerOptions.
Customize Attributes ¶
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 with a custom TRACE level:
const LevelTrace = slog.LevelDebug - 4
w := os.Stderr
logger := slog.New(tint.NewHandler(w, &tint.Options{
Level: LevelTrace,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.LevelKey && len(groups) == 0 {
level, ok := a.Value.Any().(slog.Level)
if ok && level <= LevelTrace {
return tint.Attr(13, slog.String(a.Key, "TRC"))
}
}
return a
},
}))
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
},
}),
)
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 a.Value.Kind() == slog.KindAny {
if _, ok := a.Value.Any().(error); ok {
return tint.Attr(9, a)
}
}
return a
},
}),
)
Automatically Enable Colors ¶
Colors are enabled by default. Use the Options.NoColor field to disable color output. To automatically enable colors based on terminal capabilities, use e.g., the go-isatty package:
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 package:
w := os.Stderr logger := slog.New( tint.NewHandler(colorable.NewColorable(w), nil), )
Index ¶
- Constants
- Variables
- func Attr(color uint8, attr slog.Attr) slog.Attr
- func Err(err error) slog.Attr
- func Init(level Level, addSource bool, prefix string)
- func Prefix(next slog.Handler, opts *PrefixOptions) slog.Handler
- func Printer(prefix string, level Level) iPrinter
- func Tint(w io.Writer, opts *TintOptions) slog.Handler
- type Level
- type Logger
- type PrefixOptions
- type Record
- type TintOptions
- type Value
Constants ¶
const ( LevelDebug = slog.LevelDebug LevelInfo = slog.LevelInfo LevelWarn = slog.LevelWarn LevelError = slog.LevelError )
Variables ¶
var ( Debug = slog.Debug Info = slog.Info Warn = slog.Warn Error = slog.Error DebugContext = slog.DebugContext InfoContext = slog.InfoContext WarnContext = slog.WarnContext ErrorContext = slog.ErrorContext )
var ( SetDefault = slog.SetDefault Default = slog.Default )
Functions ¶
func Attr ¶
Attr returns a tinted (colorized) slog.Attr that will be written in the specified color by the [tint.Handler]. When used with any other slog.Handler, it behaves as a plain slog.Attr.
Use the uint8 color value to specify the color of the attribute:
- 0-7: standard ANSI colors
- 8-15: high intensity ANSI colors
- 16-231: 216 colors (6×6×6 cube)
- 232-255: grayscale from dark to light in 24 steps
func Err ¶
Err returns a tinted (colorized) slog.Attr that will be written in red color by the [tint.Handler]. When used with any other slog.Handler, it behaves as
slog.Any("err", err)
func Prefix ¶
func Prefix(next slog.Handler, opts *PrefixOptions) slog.Handler
Prefix creates a new prefix logging handler. The new handler will prepend a prefix sourced from the log record's attributes to each log message before passing the record to the next handler.
func Tint ¶
func Tint(w io.Writer, opts *TintOptions) slog.Handler
Tint creates a slog.Handler that writes tinted logs to Writer w, using the default options. If opts is nil, the default options are used.
Types ¶
type PrefixOptions ¶
type PrefixOptions struct {
// A list of keys used to fetch prefix values from the log record.
PrefixKeys []string
// PrefixFormatter is a function to format the prefix of the log record.
// If it's not set, the DefaultPrefixFormatter with ColorizePrefix wrapper is used.
PrefixFormatter func(prefixes []Value) string
}
type TintOptions ¶
type TintOptions 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://pkg.go.dev/log/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
}
TintOptions for a slog.Handler that writes tinted logs. A zero TintOptions consists entirely of default values.
TintOptions can be used as a drop-in replacement for slog.HandlerOptions.