Structured Logging for the Standard Library
summary
Go 1.21 adds a structured logging package slog that aims for easy, flexible, fast logging; it ships with slog.New with TextHandler and a JSON handler, supports levels and grouped attributes, and interoperates with the existing log package. A fast API path uses Adders fast path (typed constructors like slog.String/slog.Int) and copy-by-value Records plus a small inline array so 95% calls use ≤5 attrs avoid heap allocs; benchmarks target zap-like speed and the implementation reaches that goal.
AI-written summary · about
chapters (20)
- 0:00Intro
- 0:44Logging in Go 1.20: The log package
- 1:55Structured Logging Packages
- 2:20The Problem
- 5:24Use Attrs for Speed
- 6:26Levels
- 9:03Problem: Conflicting Keys
- 12:20The Handler Interface
- 12:50Handling a Record
- 14:17History of Go Structured Logging
- 18:05Write Good Benchmarks
- 22:44Heap Allocation
- 24:52Avoiding Allocation: Variadic Arguments
- 27:22Avoiding Allocation: Pooled Buffers
- 29:49Avoiding Allocation: Passing Records
- 30:33Avoiding Allocation: Record Attributes
- 33:24Implementation of Interface Values
- 35:53String Values
- 41:13On Alternating Keys and Values
- 43:25Working with the Community: A Summary








