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
- id: C1 claim: slog records include a message, a level, and key-value attributes. source: S1 evidence: "log records include a message, a severity level"
- id: C2 claim: Each Logger is associated with a Handler. source: S1 evidence: "Each Logger is associated with a Handler"
- id: C3 claim: Top-level functions such as Info use a default Logger. source: S1 evidence: "There is a default Logger accessible through top-level functions"
- id: C4 claim: Go 1.21 added log/slog to the standard library. source: S2 evidence: "The new log/slog package in Go 1.21 brings structured logging"
- id: C5 claim: Structured logs use key-value pairs. source: S2 evidence: "Structured logs use key-value pairs"
- id: C6 claim: JSONHandler is the built-in handler for JSON output. source: S2 evidence: "install the built-in JSONHandler instead"
Sources
- [S1] log/slog package reference. Go. https://pkg.go.dev/log/slog . License: BSD-3-Clause. Accessed 2026-11-23.
- [S2] Structured Logging with slog. The Go Blog. https://go.dev/blog/slog . License: CC-BY-4.0. Accessed 2026-11-23.
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
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.},
}
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.
- go-slog.schema.json (schema; CC-BY-4.0)
- go-slog.table.json (table; CC-BY-4.0)
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.