zerolog for Go: The Complete Guide to Zero-Allocation Logging
In the world of high-load Go applications, performance is paramount. Every extra memory allocation can increase garbage collector (GC) overhead and reduce your service's throughput. Traditional logging libraries like the standard log package or the popular logrus, while convenient, put significant pressure on the heap by allocating memory for every logger call. That's where zerolog comes in — a Go logging library built with a laser focus on zero memory allocation.
zerolog gives developers a powerful, flexible, and blazingly fast API for structured JSON logging. Unlike many alternatives, zerolog uses a fluent method-chaining API and avoids interfaces and reflection in critical code paths, achieving performance close to manual string formatting. This makes it the ideal choice for microservices, API gateways, proxy servers, and any software where every microsecond and every byte of memory counts.
But zerolog isn't just fast — it's also incredibly developer-friendly. It supports log levels (debug, info, warn, error, fatal, panic), contextual logging via Logger.With(), integration with Go's context.Context, and flexible output formatting (console or JSON). In this guide, we'll walk through installing zerolog, mastering its core methods with practical examples, and exploring best practices for maximum performance.
Installation
Installing zerolog is straightforward, just like most Go libraries. Run the following command in your project root (where your go.mod file lives):
go get -u github.com/rs/zerolog/log
Once the command completes, the library will be added to your go.mod file, and you can start importing it in your code.
Quick Start: A Minimal Working Example
Let's start with the simplest example to demonstrate basic zerolog usage for outputting JSON-formatted messages.
package main
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
// Set the global log level to Debug.
// The default level is Info.
zerolog.SetGlobalLevel(zerolog.DebugLevel)
// Simple string logging.
log.Print("Hello, zerolog!")
// Logging at Info level.
log.Info().Msg("Service started successfully")
// Logging at Warn level.
log.Warn().Msg("Detected a potentially slow request")
// Logging at Error level.
log.Error().Msg("Failed to connect to the database")
}
When you run this code, you'll see JSON strings in the console containing the timestamp, log level, message, and other metadata. Notice the method chaining: log.Info().Msg("..."). That's the fluent API that keeps your code clean and concise.
Core Methods and Functions
zerolog provides a rich set of methods for flexible and performant logging. Let's explore 8 key ones.
1. log.Info(), log.Debug(), log.Warn(), log.Error(), log.Fatal(), log.Panic()
Signature: func Info() *Event (and similarly for other levels)
What it does: These methods create a new logging event with the corresponding severity level. They return a pointer to the Event struct, which you can then enrich with additional fields using other methods (e.g., Str(), I