Structured logging with log/slog
Answer: log/slog, added in Go 1.21, brings structured logging to the standard library.
Quick start
import "log/slog"
slog.Info("hello, world", "user", "jba")
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
logger.Warn("slow request", "path", "/api", "ms", 812)
JSON output looks like {"time":"...","level":"INFO","msg":"hello, world","user":"jba"}.
Concepts
- Levels: Debug, Info, Warn, Error.
- For more control over the output format, create a logger with a different handler. (S2)
Claims
- id: C1 claim: log/slog in Go 1.21 brings structured logging to the standard library. source: S1 evidence: "The new log/slog package in Go 1.21 brings structured logging to the standard"
- id: C2 claim: Structured logs use key-value pairs. source: S1 evidence: "library. Structured logs use key-value pairs so they can be parsed, filtered,"
- id: C3 claim: Everything becomes a key-value pair in text output. source: S1 evidence: "Everything has been turned into a key-value pair, with strings quoted as needed to preserve"
- id: C4 claim: For more control over output, create a logger with a different handler. source: S2 evidence: "For more control over the output format, create a logger with a different handler."
Sources
- [S1] Structured Logging with slog - The Go Programming Language. The Go Authors (go.dev). https://go.dev/blog/slog . License: CC BY 4.0 (go.dev text). Accessed 2026-11-04.
- [S2] slog package - log/slog - Go Packages. The Go Authors (go.dev). https://pkg.go.dev/log/slog . License: CC BY 4.0 (go.dev text). Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Go Authors (go.dev), Structured Logging with slog - The Go Programming Language, CC BY 4.0 (go.dev text); The Go Authors (go.dev), slog package - log/slog - Go Packages, CC BY 4.0 (go.dev text).
Cite this
iinventedlight. 2026. "Structured logging with log/slog." iinventedlight Library, iil:note:zjdgwzq3, version of 2026-11-04. https://iinventedlight.com/library/notes/go-structured-logging-slog/. License: CC-BY-4.0.
@misc{iil_note_zjdgwzq3,
author = {{iinventedlight}},
title = {{Structured logging with log/slog}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/go-structured-logging-slog/},
note = {Permanent ID iil:note:zjdgwzq3. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/go-structured-logging-slog.md.