Zerolog is a Go package focused on fast JSON (or CBOR) log output with a chaining API. It targets low allocation logging and integrates with context, HTTP handlers, and log/slog.
Overview
Zerolog provides structured logging for Go applications where events are encoded as JSON by default. Its chaining API is designed to avoid allocations and reflection on the hot path compared with many traditional loggers.
Developers attach typed key-value fields to events, select levels from trace through panic, and must end chains with Msg or Msgf for output to occur. A global logger can write to standard error at trace unless the global level is raised or logging is disabled entirely.
Features include sampling, hooks, sub-loggers with extra context, optional pretty console output, stack traces for errors, multi-writer output, and HTTP middleware via the hlog package. Binary CBOR encoding is available behind a build tag, and a slog.Handler bridges the standard library slog API to zerolog.
Auxiliary writers such as ConsoleWriter trade efficiency for readability during development. Benchmarks in the project compare allocation counts and latency against other Go logging libraries.
Key features
- JSON and CBOR encoding
- Chaining leveled API
- Context and slog integration
- net/http hlog helpers
- Hooks and sampling
- ConsoleWriter pretty output
- Multi-level writers
- Typed contextual fields
Use cases
- Production JSON service logs
- Low-allocation Go microservices
- HTTP request logging middleware
- Unified slog and zerolog pipelines
- Development-friendly console traces