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.

Evaluated Mar 06, 2026 (0d ago) vGo 1.21+
Homepage ↗ Repo ↗ Developer Tools go logging slog structured json stdlib standard-library handlers
⚙ Agent Friendliness
68
/ 100
Can an agent use this?
🔒 Security
98
/ 100
Is it safe for agents?
⚡ Reliability
95
/ 100
Does it work consistently?

Score Breakdown

⚙ Agent Friendliness

MCP Quality
--
Documentation
88
Error Messages
82
Auth Simplicity
100
Rate Limits
100

🔒 Security

TLS Enforcement
100
Auth Strength
100
Scope Granularity
100
Dep. Hygiene
100
Secret Handling
90

Standard library. Avoid logging sensitive data (credentials, PII) — slog makes it easy to add structured attributes; be careful about what gets included.

⚡ Reliability

Uptime/SLA
98
Version Stability
95
Breaking Changes
95
Error Recovery
92
AF Security 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

REST API
No
GraphQL
No
gRPC
No
MCP Server
No
SDK
Yes
Webhooks
No

Authentication

Methods: none
OAuth: No Scopes: No

Standard library package. No authentication required.

Pricing

Model: open_source
Free tier: Yes
Requires CC: No

BSD license. Part of the Go standard library.

Agent Metadata

Pagination
none
Idempotent
Full
Retry Guidance
Not documented

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).

$99

Scores are editorial opinions as of 2026-03-06.

5215
Packages Evaluated
26151
Need Evaluation
173
Need Re-evaluation
Community Powered