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


Log levels

I'm launching a new live course: **Idiomatic Testing in Go**. The course begins May 5. Early-bird pricing is in effect until April 28. Why Go avoids assert libraries Better alternatives to mocks Make writing tests fun! See pricing & reserve → log/slog provides some rather sophisticated capabilities around log levels. We’ll get into it, but the good news is, you don’t need to care about how sophisticated it can get, if you don’t care.


Logging common fields

It’s common that you’ll want to include certain attributes in all logs in an application or component. log/slog makes this pretty easy. Overview … Some attributes are common to many log calls. For example, you may wish to include the URL or trace identifier of a server request with all log events arising from the request. Rather than repeat the attribute with every log call, you can use Logger.With to construct a new Logger containing the attributes:


Handler configuration

The default slog handlers are quite configurable. Overview … Both TextHandler and JSONHandler can be configured with HandlerOptions. There are options for setting the minimum level (see Levels, below), displaying the source file and line of the log call, and modifying attributes before they are logged. While HandlerOptions only exposes three fields: AddSource bool Level Leveler ReplaceAttr func(groups []string, a Attr) Attr The last one provides an immense amount of flexibility, letting you filter, replace, or augment log key/value pairs as they are processed.

Get daily content like this in your inbox!

Subscribe