← All notes

log/slog: structured logging in the Go standard library

Answer: Package slog provides structured logging: each record has a message, a level, and key-value attributes. A Logger builds a Record and hands it to a Handler. The log/slog package in Go 1.21 brings structured logging to the standard library.

Records, loggers, and handlers

In slog, a log record carries a message, a severity level, and other attributes expressed as key-value pairs.

Info and Error are methods for reporting events of interest.

Each Logger is associated with a Handler, which receives the Record and decides what to do with it.

Top-level helpers such as Info and Error call the corresponding methods on a default Logger.

A log record consists of a time, a level, a message, and key-value pairs.

Why the standard library grew a structured logger

Go 1.21 introduced log/slog so structured logging would live in the standard library.

Key-value pairs are the point: they can be parsed, filtered, searched, and analyzed quickly and reliably.

The older log package has been present since Go's first release, but the annual survey kept ranking structured logging high.

Text and JSON handlers

TextHandler writes the record as key=value text.

For JSON output, install the built-in JSONHandler instead.

Anyone can write a handler by implementing the slog.Handler interface.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go, log/slog package reference; The Go Blog, Structured Logging with slog. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. CC-BY-4.0 (https://go.dev/copyright). Copyright The Go Authors. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go, log/slog package reference; The Go Blog, Structured Logging with slog. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. CC-BY-4.0 (https://go.dev/copyright). Copyright The Go Authors.

Cite this

Permanent ID: iil:note:5xbmnddc

iinventedlight. 2026. "log/slog: structured logging in the Go standard library." iinventedlight Library, iil:note:5xbmnddc, version of 2026-11-23. https://iinventedlight.com/library/notes/go-slog/. License: CC-BY-4.0.

@misc{iil_note_5xbmnddc,
  author = {{iinventedlight}},
  title = {{log/slog: structured logging in the Go standard library}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-23},
  url = {https://iinventedlight.com/library/notes/go-slog/},
  note = {Permanent ID iil:note:5xbmnddc. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-slog.md.