The default handler

April 7, 2026

Much like the older log package, log/slog ships with a “default logger”. You can use this logger without doing any configuration, just by calling any of the package-level logging functions:

slog.Error("oh noes!")

While you’d probably never want to use the default logger in a serious server application, it can be a convenience for small or throw-away utilities. But how does it work? That’s today’s topic!

Overview

The default handler formats the log record’s message, time, level, and attributes as a string and passes it to the log package.

2022/11/08 15:28:26 INFO hello count=3

So that’s doubly convenient—not only does it log things in a reasonable format, but it also passes them to the log package for backward compatibility. This means that if your application already takes advantage of the default log package’s logger, you don’t need to do any additional configuration to start using log/slog. Sweet!

But wait… how does the default log package’s logger work? Well, it’s pretty limited (which is, of course, largely why log/slog was added to replace it!) By default, it simply writes to STDERR. Though you can configure the default log.Logger… a little bit—mainly by redirecting where those logs go (e.g. to a file). But that’s a good segue into the default logger configuration we do care about—you can also configure the log/slog default logger, which we’ll talk about tomorrow!


Share this

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

Unsure? Browse the archive .

Related Content


The global logger

I’m going to do something I haven’t done before in these stdlib tours, and that is skip over a huge section of the GoDoc. That’s becase a huge section here is entirely redundant with what comes later: func Debug func Debug(msg string, args ...any) Debug calls Logger.Debug on the default logger. And we have virtually identical entries for each of the following: DebugContext Error ErrorContext Info InfoContext Log LogAttrs Warn WarnContext Each of these is simply an alias to the identically named method on the default logger.


slog Handler methods

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.


Attribute equality

func (Attr) Equal func (a Attr) Equal(b Attr) bool Equal reports whether a and b have equal keys and values. That’s a pretty obvious, and opaque statement. How is equality determined? We have to look at the source to determine: func (a Attr) Equal(b Attr) bool { return a.Key == b.Key && a.Value.Equal(b.Value) } Okay, so it returns true if the keys are equal (that’s simple—they’re just strings) AND if the values are equal.

Get daily content like this in your inbox!

Subscribe