0Pricing
Go Academy · Lesson

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) bool
  • Handle(ctx, record) error
  • WithAttrs(attrs) Handler
  • WithGroup(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

  1. Why Structured Logs
  2. slog Handlers
  3. Log Levels and Attributes
  4. Custom Handlers
← Back to Go Academy