zerolog
Zero Allocation JSON Logger
A Go logging library that writes structured JSON log entries using zero memory allocations, making it ideal for high-traffic services where logging speed matters.
Zerolog is a Go logging library designed for speed and low overhead. Its main claim is that it produces zero memory allocations when writing log entries, which matters for high-throughput services where even small per-request costs add up. Log output is structured JSON by default, meaning each log line is a machine-readable object rather than a free-form string.
The API uses method chaining: you call something like log.Info().Str("user", userId).Int("status", 200).Msg("request complete") and zerolog builds the JSON object and writes it in one pass. The chained calls let you attach any number of typed fields (strings, integers, floats, booleans, errors, durations) to each log entry, which makes logs easier to search and filter in log aggregation tools. Log levels go from trace and debug at the low end up through info, warn, error, fatal, and panic.
For development, zerolog includes a ConsoleWriter that formats output as readable colored text instead of JSON. The README explicitly notes this mode is slower and not intended for production. The library also supports sampling (writing only a fraction of log messages at a given level to reduce volume), hooks for custom processing on each log event, and optional stack traces on errors via the pkg/errors integration.
Integration hooks exist for Go's standard library context package (to carry a logger through request chains) and for the standard net/http package. Zerolog also integrates with the log/slog interface introduced in Go 1.21, so you can swap in zerolog as the backend for code written against the standard interface.
Install via go get. The project is MIT-licensed and maintains benchmarks comparing its performance against other Go loggers.
Where it fits
- Add fast, structured JSON logging to a Go web service with minimal impact on request throughput.
- Attach typed fields like user IDs, status codes, and durations to each log entry for easy filtering in log tools.
- Use the colored console output mode during local development for human-readable logs, then switch to JSON in production.
- Integrate zerolog as the backend for code already written against Go's standard log/slog interface.