API Reference

zerolog

Zero-allocation structured JSON logging for Go with a fluent event API and low-overhead sampling.

Official Documentation ↗
CompatibilityThis documentation covers version v1.33. Check the official docs for the latest API surface.

Installation

gogo get github.com/rs/zerolog

Methods

Zero-allocation structured JSON logging for Go with a fluent event API and low-overhead sampling.

Overview

zerolog emits structured JSON with essentially no allocations. Each log call starts a typed Event, chains field methods, and terminates with Msg, writing bytes from a pooled buffer straight to the destination.

Loggers carry bound context through With, ConsoleWriter switches to human-friendly output for development, and burst samplers cap noisy paths. The global log.Logger makes adoption incremental, and hook integration covers custom enrichment like tracing.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • log.Info / log.Error — Chains typed field methods onto an event and emits JSON when Msg terminates the chain.
  • zerolog.New / With — Builds a logger with bound context fields; child loggers inherit them cheaply.
  • ConsoleWriter — Pretty terminal rendering for development while keeping the JSON pipeline for production.
  • levels / sampling — Level filtering and burst sampling that caps the cost of hot debug paths.

Usage Notes

  • Terminate every event with Msg or Send; run zerolog-lint in CI to catch omissions.
  • Bind service and trace fields on child loggers instead of repeating them per call.
  • Sample high-frequency debug paths before they dominate volume and cost.

Common Pitfalls

  • Forgetting the terminal Msg silently drops the whole event.
  • ConsoleWriter in production wastes CPU and loses the JSON contract for pipelines.
  • The global logger is a package variable; libraries should accept a logger instead of writing to it.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/rs/zerolog@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:53:33.973Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

What happens if I forget Msg?
The event is never written: fields accumulate on the Event and it is discarded. Linters such as zerolog-lint catch the missing terminal call, which is the commonest zerolog mistake.
How does zerolog stay zero-allocation?
Events write into a pooled buffer and fields serialize directly to bytes without boxing into interfaces. That design is why the fluent API exists: each typed method appends without allocating.
How do I use it with the standard log package?
zerolog has adapters: zerolog.StdoutWriter bridges via log.SetOutput, and the log.Logger global can be swapped wholesale. Redirecting stdlib logs lets third-party packages join your JSON stream.