slog.Handler

September 9, 2026

I’m probably doing this in backwards order, but that’s the order the GoDoc presents it…

Now that we’ve looked at each of the methods of the Handler interface, we come to its general description:

type Handler

A Handler handles log records produced by a Logger.

A typical handler may print log records to standard error, or write them to a file or database, or perhaps augment them with additional attributes and pass them on to another handler.

Any of the Handler’s methods may be called concurrently with itself or with other methods. It is the responsibility of the Handler to manage this concurrency.

Users of the slog package should not invoke Handler methods directly. They should use the methods of Logger instead.

Before implementing your own handler, consult https://go.dev/s/slog-handler-guide.

Three important things to take away from this:

  1. The handler must handle any concurrency issues, such that it’s safe to call any methods concurrently.
  2. Don’t call handler methods directly, as a user of the slog package.
  3. RTFM 😂 If you’re going to implement a handler, don’t rely merely on this documentation. Read the handler guide.

I don’t plan to disect the handler guide here. That’s a pretty niche thing, and would be of limited interest to most readers, I expect. But if you disagree, let me know. Maybe I can go into greater detail on handler authoring in a longer-form blog post or something, if it’s interesting to folks.


Share this

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

Unsure? Browse the archive .

Related Content


slog.Handler.WithGroup

I hope everyone had a good Labor Day weekend… even if you’re not in the US and had to/got to labor on Monday. Speaking of labor, I’m looking for new clients! If you could use some world class Go expertise on your project, please reach out! type Handler type Handler interface { … // WithGroup returns a new Handler with the given group appended to // the receiver's existing groups. // The keys of all subsequent attributes, whether added by With or in a // Record, should be qualified by the sequence of group names.


slog.Handler.WithAttrs

type Handler type Handler interface { … // WithAttrs returns a new Handler whose attributes consist of // both the receiver's attributes and the arguments. // The Handler owns the slice: it may retain, modify or discard it. WithAttrs(attrs []Attr) Handler This is likely the most interesting and subtle method of the slog.Handler interface. The important thing to note is that the handler is not mutated in place. Rather, a new handler is meant to be returned.


slog.Handler.Handle()

I had not intended to take a month off, but then GopherCon came, and life happened… but now I’m back, and ready to rock and roll again… Today we’re to the core of the Handler interface: the Handle method. type Handler type Handler interface { … // Handle handles the Record. // It will only be called when Enabled returns true. // The Context argument is as for Enabled. // It is present solely to provide Handlers access to the context's values.

Get daily content like this in your inbox!

Subscribe