Introduction
Zerolog is a zero-allocation JSON logger for Go designed for applications where logging overhead must be negligible. It writes structured JSON directly to an io.Writer without intermediate allocations, making it one of the fastest loggers available in the Go ecosystem while maintaining a clean fluent API.
What Zerolog Does
- Produces structured JSON log output with zero heap allocations per log event
- Provides a fluent chaining API for building log events with typed fields
- Supports leveled logging with compile-time level disabling for zero-cost disabled levels
- Offers context loggers for adding common fields across request scopes
- Includes a pretty-printing console writer for human-readable development output
Architecture Overview
Zerolog writes JSON directly to a byte buffer using manual JSON encoding (no encoding/json or reflection). Each log event is an Event object that appends key-value pairs as raw JSON bytes to an internal buffer. When Msg() or Send() is called, the buffer is flushed to the underlying io.Writer in a single write call. The Event objects are pooled via sync.Pool to eliminate allocations entirely on the hot path.
Self-Hosting & Configuration
- Install:
go get github.com/rs/zerolog - Create a logger:
zerolog.New(os.Stdout).With().Timestamp().Logger() - Set global level:
zerolog.SetGlobalLevel(zerolog.InfoLevel) - Use
zerolog.ConsoleWriter{Out: os.Stderr}for colorized dev output - Create child loggers with
logger.With().Str("component", "handler").Logger()
Key Features
- True zero allocation: benchmarks show 0 allocs/op for all log levels
- Fluent API with typed methods (Str, Int, Bool, Err, Dur) prevents type mismatches
- Context-based child loggers inherit fields without copying
- Pretty console output for development with optional color and time formatting
- Integration packages for net/http, gRPC interceptors, and Hlog middleware
Comparison with Similar Tools
- Zap — also high-performance with near-zero allocations; Zerolog achieves true zero allocs with a simpler chaining API
- Logrus — feature-rich but uses reflection; Zerolog is 10x+ faster in benchmarks
- slog (stdlib) — standard since Go 1.21 with handler interface; Zerolog is faster and has richer field type support
- Apex Log — clean interface-based design; Zerolog prioritizes allocation-free performance
- Go kit log — composable logger for microservices; Zerolog is standalone and faster
FAQ
Q: How does Zerolog achieve zero allocations? A: It writes JSON bytes directly to a pooled buffer using manual encoding, avoiding reflection, interface boxing, and map allocations.
Q: Can I use Zerolog with the standard library log package?
A: Yes. Use log.Logger = zerolog.New(os.Stdout) with the log adapter to redirect stdlib log calls.
Q: Does Zerolog support log rotation? A: Zerolog writes to any io.Writer. Use lumberjack or a similar package as the writer for rotation.
Q: How do I add request-scoped fields in an HTTP handler? A: Use the hlog middleware package to attach a zerolog.Logger to the request context with common fields like request ID.