slog Handler methods

July 25, 2026

slog.Handler is an interface type. We’re going to look at each method on that interface in turn. Understanding these methods is valuable if you’re ever implementing your own handler. If you’re only using slog to produce logs using existing handlers, you could skip this part… though maybe you’ll learn something interesting anyway!

type Handler

type Handler interface {
	// Enabled reports whether the handler handles records at the given level.
	// The handler ignores records whose level is lower.
	// It is called early, before any arguments are processed,
	// to save effort if the log event should be discarded.
	// If called from a Logger method, the first argument is the context
	// passed to that method, or context.Background() if nil was passed
	// or the method does not take a context.
	// The context is passed so Enabled can use its values
	// to make a decision.
	Enabled(context.Context, Level) bool

As a handler author, you’ll absolutely want to lean on Enabled! The obvious use case is to return false if the handler is configured at a lower level than Level. But the fact that context.Context is passed makes it much more flexible (and potentially confusing) as well.

func (h *myHandler) Enabled(ctx context.Context, lvl slog.Level) bool {
  if _, ctx.Value("environment").(string) == "demo" {
    // Don't pollute logs with demo data
    return false
  }
  return h.Level >= lvl
}

Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Get daily content like this in your inbox!

Subscribe