Custom Handlers
Build your own log output.
When Built-In Handlers Are Not Enough
Text and JSON handlers cover most needs, but sometimes you want custom output: colorized terminals, sending to a metrics system, or redacting secrets. You build a custom Handler.
The Handler Interface
A handler implements four methods:
Enabled(ctx, level) boolHandle(ctx, record) errorWithAttrs(attrs) HandlerWithGroup(name) Handler
type Handler interface {
Enabled(context.Context, slog.Level) bool
Handle(context.Context, slog.Record) error
WithAttrs(attrs []slog.Attr) slog.Handler
WithGroup(name string) slog.Handler
}All lessons in this course
- Why Structured Logs
- slog Handlers
- Log Levels and Attributes
- Custom Handlers