log/slog (Go structured logging)
Go standard library structured logging package introduced in Go 1.21. Provides leveled (Debug, Info, Warn, Error) structured logging with key-value pairs, JSON and text handlers, and a handler interface for custom backends. Replaces the unstructured log package for applications needing structured logging. Designed to be fast, zero-allocation in common cases, and interoperable with third-party logging backends (zap, logrus) via handler adapters.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
Standard library. Avoid logging sensitive data (credentials, PII) — slog makes it easy to add structured attributes; be careful about what gets included.
⚡ Reliability
Best When
You're writing Go 1.21+ services and want structured logging without external dependencies — slog is the new standard for Go structured logging.
Avoid When
You need maximum throughput (zap), Go < 1.21, or complex logging pipelines with rich middleware (logrus hooks).
Use Cases
- • Add structured JSON logging to Go services using standard library slog without external dependencies
- • Replace log.Printf() calls in Go agent services with slog's leveled, structured logging for better observability
- • Implement custom slog handlers that route log records to third-party backends (Datadog, CloudWatch, Loki) via the Handler interface
- • Log agent execution context (request ID, user ID, trace ID) as structured attributes that survive log aggregation queries
- • Use slog's Group() and With() methods to attach context to all logs within a request handler scope
Not For
- • Go < 1.21 — slog is stdlib only from Go 1.21; use golang.org/x/exp/slog backport for older versions
- • Teams needing maximum logging performance — zap is still faster for extreme throughput; slog is fast but not fastest
- • Complex log pipelines with sampling, enrichment, and routing — logrus + hooks or zap + cores offer richer middleware
Interface
Authentication
Standard library package. No authentication required.
Pricing
BSD license. Part of the Go standard library.
Agent Metadata
Known Gotchas
- ⚠ slog uses a default global logger — set the default with slog.SetDefault() at startup; package-level slog.Info() etc. use this global
- ⚠ slog attributes are ordered key-value pairs in the output — duplicate keys are allowed (no deduplication) which can cause confusion in log queries
- ⚠ Context-aware logging requires slog.InfoContext(ctx, ...) variants — context propagation for trace ID injection needs ctx passed explicitly
- ⚠ Custom Handler implementations must handle concurrent calls safely — the Handle() method may be called from multiple goroutines simultaneously
- ⚠ slog.Group() creates nested attribute groups in JSON output — field access in log queries requires knowing the group path (e.g., 'request.method')
- ⚠ Level filtering with slog.Level comparison: LevelDebug < LevelInfo < LevelWarn < LevelError — ensure handler's minimum level is set correctly
Alternatives
Full Evaluation Report
Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for log/slog (Go structured logging).
Scores are editorial opinions as of 2026-03-06.