* Update dependencies and build to go1.22 * Fix api changes wrt to dependencies * Update golangci config
24 KiB
Zero Allocation JSON Logger
The zerolog package provides a fast and simple logger dedicated to JSON output.
Zerolog’s API is designed to provide both a great developer experience and stunning performance. Its unique chaining API allows zerolog to write JSON (or CBOR) log events by avoiding allocations and reflection.
Uber’s zap library pioneered this approach. Zerolog is taking this concept to the next level with a simpler to use API and even better performance.
To keep the code base and the API simple, zerolog focuses on
efficient structured logging only. Pretty logging on the console is made
possible using the provided (but inefficient) zerolog.ConsoleWriter
.
Who uses zerolog
Find out who uses zerolog and add your company / project to the list.
Features
- Blazing fast
- Low to zero allocation
- Leveled logging
- Sampling
- Hooks
- Contextual fields
context.Context
integration- Integration with
net/http
- JSON and CBOR encoding formats
- Pretty logging for development
- Error Logging (with optional Stacktrace)
Installation
go get -u github.com/rs/zerolog/log
Getting Started
Simple Logging Example
For simple logging, import the global logger package github.com/rs/zerolog/log
package main
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
// UNIX Time is faster and smaller than most timestamps
.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog
.Print("hello world")
log}
// Output: {"time":1516134303,"level":"debug","message":"hello world"}
Note: By default log writes to
os.Stderr
Note: The default log level forlog.Print
is debug
Contextual Logging
zerolog allows data to be added to log messages in the form of key:value pairs. The data added to the message adds “context” about the log event that can be critical for debugging as well as myriad other purposes. An example of this is below:
package main
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog
.Debug().
log("Scale", "833 cents").
Str("Interval", 833.09).
Float64("Fibonacci is everywhere")
Msg
.Debug().
log("Name", "Tom").
Str()
Send}
// Output: {"level":"debug","Scale":"833 cents","Interval":833.09,"time":1562212768,"message":"Fibonacci is everywhere"}
// Output: {"level":"debug","Name":"Tom","time":1562212768}
You’ll note in the above example that when adding contextual fields, the fields are strongly typed. You can find the full list of supported fields here
Leveled Logging
Simple Leveled Logging Example
package main
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog
.Info().Msg("hello world")
log}
// Output: {"time":1516134303,"level":"info","message":"hello world"}
It is very important to note that when using the zerolog chaining API, as shown above (
log.Info().Msg("hello world"
), the chain must have either theMsg
orMsgf
method call. If you forget to add either of these, the log will not occur and there is no compile time error to alert you of this.
zerolog allows for logging at the following levels (from highest to lowest):
- panic (
zerolog.PanicLevel
, 5) - fatal (
zerolog.FatalLevel
, 4) - error (
zerolog.ErrorLevel
, 3) - warn (
zerolog.WarnLevel
, 2) - info (
zerolog.InfoLevel
, 1) - debug (
zerolog.DebugLevel
, 0) - trace (
zerolog.TraceLevel
, -1)
You can set the Global logging level to any of these options using
the SetGlobalLevel
function in the zerolog package, passing
in one of the given constants above, e.g. zerolog.InfoLevel
would be the “info” level. Whichever level is chosen, all logs with a
level greater than or equal to that level will be written. To turn off
logging entirely, pass the zerolog.Disabled
constant.
Setting Global Log Level
This example uses command-line flags to demonstrate various outputs depending on the chosen log level.
package main
import (
"flag"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog:= flag.Bool("debug", false, "sets log level to debug")
debug
.Parse()
flag
// Default level for this example is info, unless debug flag is present
.SetGlobalLevel(zerolog.InfoLevel)
zerologif *debug {
.SetGlobalLevel(zerolog.DebugLevel)
zerolog}
.Debug().Msg("This message appears only when log level set to Debug")
log.Info().Msg("This message appears when log level set to Debug or Info")
log
if e := log.Debug(); e.Enabled() {
// Compute log output only if enabled.
:= "bar"
value .Str("foo", value).Msg("some debug message")
e}
}
Info Output (no flag)
$ ./logLevelExample
{"time":1516387492,"level":"info","message":"This message appears when log level set to Debug or Info"}
Debug Output (debug flag set)
$ ./logLevelExample -debug
{"time":1516387573,"level":"debug","message":"This message appears only when log level set to Debug"}
{"time":1516387573,"level":"info","message":"This message appears when log level set to Debug or Info"}
{"time":1516387573,"level":"debug","foo":"bar","message":"some debug message"}
Logging without Level or Message
You may choose to log without a specific level by using the
Log
method. You may also write without a message by setting
an empty string in the msg string
parameter of the
Msg
method. Both are demonstrated in the example below.
package main
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog
.Log().
log("foo", "bar").
Str("")
Msg}
// Output: {"time":1494567715,"foo":"bar"}
Error Logging
You can log errors using the Err
method
package main
import (
"errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog
:= errors.New("seems we have an error here")
err .Error().Err(err).Msg("")
log}
// Output: {"level":"error","error":"seems we have an error here","time":1609085256}
The default field name for errors is
error
, you can change this by settingzerolog.ErrorFieldName
to meet your needs.
Error Logging with Stacktrace
Using github.com/pkg/errors
, you can add a formatted
stacktrace to your errors.
package main
import (
"github.com/pkg/errors"
"github.com/rs/zerolog/pkgerrors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
zerolog
:= outer()
err .Error().Stack().Err(err).Msg("")
log}
func inner() error {
return errors.New("seems we have an error here")
}
func middle() error {
:= inner()
err if err != nil {
return err
}
return nil
}
func outer() error {
:= middle()
err if err != nil {
return err
}
return nil
}
// Output: {"level":"error","stack":[{"func":"inner","line":"20","source":"errors.go"},{"func":"middle","line":"24","source":"errors.go"},{"func":"outer","line":"32","source":"errors.go"},{"func":"main","line":"15","source":"errors.go"},{"func":"main","line":"204","source":"proc.go"},{"func":"goexit","line":"1374","source":"asm_amd64.s"}],"error":"seems we have an error here","time":1609086683}
zerolog.ErrorStackMarshaler must be set in order for the stack to output anything.
Logging Fatal Messages
package main
import (
"errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
:= errors.New("A repo man spends his life getting into tense situations")
err := "myservice"
service
.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog
.Fatal().
log(err).
Err("service", service).
Str("Cannot start %s", service)
Msgf}
// Output: {"time":1516133263,"level":"fatal","error":"A repo man spends his life getting into tense situations","service":"myservice","message":"Cannot start myservice"}
// exit status 1
NOTE: Using
Msgf
generates one allocation even when the logger is disabled.
Create logger instance to manage different outputs
:= zerolog.New(os.Stderr).With().Timestamp().Logger()
logger
.Info().Str("foo", "bar").Msg("hello world")
logger
// Output: {"level":"info","time":1494567715,"message":"hello world","foo":"bar"}
Sub-loggers let you chain loggers with additional context
:= log.With().
sublogger ("component", "foo").
Str()
Logger.Info().Msg("hello world")
sublogger
// Output: {"level":"info","time":1494567715,"message":"hello world","component":"foo"}
Pretty logging
To log a human-friendly, colorized output, use
zerolog.ConsoleWriter
:
.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log
.Info().Str("foo", "bar").Msg("Hello world")
log
// Output: 3:04PM INF Hello World foo=bar
To customize the configuration and formatting:
:= zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
output .FormatLevel = func(i interface{}) string {
outputreturn strings.ToUpper(fmt.Sprintf("| %-6s|", i))
}
.FormatMessage = func(i interface{}) string {
outputreturn fmt.Sprintf("***%s****", i)
}
.FormatFieldName = func(i interface{}) string {
outputreturn fmt.Sprintf("%s:", i)
}
.FormatFieldValue = func(i interface{}) string {
outputreturn strings.ToUpper(fmt.Sprintf("%s", i))
}
:= zerolog.New(output).With().Timestamp().Logger()
log
.Info().Str("foo", "bar").Msg("Hello World")
log
// Output: 2006-01-02T15:04:05Z07:00 | INFO | ***Hello World**** foo:BAR
Sub dictionary
.Info().
log("foo", "bar").
Str("dict", zerolog.Dict().
Dict("bar", "baz").
Str("n", 1),
Int).Msg("hello world")
// Output: {"level":"info","time":1494567715,"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"}
Customize automatic field names
.TimestampFieldName = "t"
zerolog.LevelFieldName = "l"
zerolog.MessageFieldName = "m"
zerolog
.Info().Msg("hello world")
log
// Output: {"l":"info","t":1494567715,"m":"hello world"}
Add contextual fields to the global logger
.Logger = log.With().Str("foo", "bar").Logger() log
Add file and line number to log
Equivalent of Llongfile
:
.Logger = log.With().Caller().Logger()
log.Info().Msg("hello world")
log
// Output: {"level": "info", "message": "hello world", "caller": "/go/src/your_project/some_file:21"}
Equivalent of Lshortfile
:
.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
zerolog:= file
short for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
= file[i+1:]
short break
}
}
= short
file return file + ":" + strconv.Itoa(line)
}
.Logger = log.With().Caller().Logger()
log.Info().Msg("hello world")
log
// Output: {"level": "info", "message": "hello world", "caller": "some_file:21"}
Thread-safe, lock-free, non-blocking writer
If your writer might be slow or not thread-safe and you need your log
producers to never get slowed down by a slow writer, you can use a
diode.Writer
as follows:
:= diode.NewWriter(os.Stdout, 1000, 10*time.Millisecond, func(missed int) {
wr .Printf("Logger Dropped %d messages", missed)
fmt})
:= zerolog.New(wr)
log .Print("test") log
You will need to install code.cloudfoundry.org/go-diodes
to use this feature.
Log Sampling
:= log.Sample(&zerolog.BasicSampler{N: 10})
sampled .Info().Msg("will be logged every 10 messages")
sampled
// Output: {"time":1494567715,"level":"info","message":"will be logged every 10 messages"}
More advanced sampling:
// Will let 5 debug messages per period of 1 second.
// Over 5 debug message, 1 every 100 debug messages are logged.
// Other levels are not sampled.
:= log.Sample(zerolog.LevelSampler{
sampled : &zerolog.BurstSampler{
DebugSampler: 5,
Burst: 1*time.Second,
Period: &zerolog.BasicSampler{N: 100},
NextSampler},
})
.Debug().Msg("hello world")
sampled
// Output: {"time":1494567715,"level":"debug","message":"hello world"}
Hooks
type SeverityHook struct{}
func (h SeverityHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
if level != zerolog.NoLevel {
.Str("severity", level.String())
e}
}
:= log.Hook(SeverityHook{})
hooked .Warn().Msg("")
hooked
// Output: {"level":"warn","severity":"warn"}
Pass a sub-logger by context
:= log.With().Str("component", "module").Logger().WithContext(ctx)
ctx
.Ctx(ctx).Info().Msg("hello world")
log
// Output: {"component":"module","level":"info","message":"hello world"}
Set as standard logger output
:= zerolog.New(os.Stdout).With().
log ("foo", "bar").
Str()
Logger
.SetFlags(0)
stdlog.SetOutput(log)
stdlog
.Print("hello world")
stdlog
// Output: {"foo":"bar","message":"hello world"}
context.Context integration
Go contexts are commonly passed throughout Go code, and this can help
you pass your Logger into places it might otherwise be hard to inject.
The Logger
instance may be attached to Go context
(context.Context
) using
Logger.WithContext(ctx)
and extracted from it using
zerolog.Ctx(ctx)
. For example:
func f() {
:= zerolog.New(os.Stdout)
logger := context.Background()
ctx
// Attach the Logger to the context.Context
= logger.WithContext(ctx)
ctx (ctx)
someFunc}
func someFunc(ctx context.Context) {
// Get Logger from the go Context. if it's nil, then
// `zerolog.DefaultContextLogger` is returned, if
// `DefaultContextLogger` is nil, then a disabled logger is returned.
:= zerolog.Ctx(ctx)
logger .Info().Msg("Hello")
logger}
A second form of context.Context
integration allows you
to pass the current context.Context into the logged event, and retrieve
it from hooks. This can be useful to log trace and span IDs or other
information stored in the go context, and facilitates the unification of
logging and tracing in some systems:
type TracingHook struct{}
func (h TracingHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
:= e.GetCtx()
ctx := getSpanIdFromContext(ctx) // as per your tracing framework
spanId .Str("span-id", spanId)
e}
func f() {
// Setup the logger
:= zerolog.New(os.Stdout)
logger = logger.Hook(TracingHook{})
logger
:= context.Background()
ctx // Use the Ctx function to make the context available to the hook
.Info().Ctx(ctx).Msg("Hello")
logger}
Integration with
net/http
The github.com/rs/zerolog/hlog
package provides some
helpers to integrate zerolog with http.Handler
.
In this example we use alice to install logger for better readability.
:= zerolog.New(os.Stdout).With().
log ().
Timestamp("role", "my-service").
Str("host", host).
Str()
Logger
:= alice.New()
c
// Install the logger handler with default output on the console
= c.Append(hlog.NewHandler(log))
c
// Install some provided extra handler to set some request's context fields.
// Thanks to that handler, all our logs will come with some prepopulated fields.
= c.Append(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
c .FromRequest(r).Info().
hlog("method", r.Method).
Str("url", r.URL).
Stringer("status", status).
Int("size", size).
Int("duration", duration).
Dur("")
Msg}))
= c.Append(hlog.RemoteAddrHandler("ip"))
c = c.Append(hlog.UserAgentHandler("user_agent"))
c = c.Append(hlog.RefererHandler("referer"))
c = c.Append(hlog.RequestIDHandler("req_id", "Request-Id"))
c
// Here is your final handler
:= c.Then(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h // Get the logger from the request's context. You can safely assume it
// will be always there: if the handler is removed, hlog.FromRequest
// will return a no-op logger.
.FromRequest(r).Info().
hlog("user", "current user").
Str("status", "ok").
Str("Something happened")
Msg
// Output: {"level":"info","time":"2001-02-03T04:05:06Z","role":"my-service","host":"local-hostname","req_id":"b4g0l5t6tfid6dtrapu0","user":"current user","status":"ok","message":"Something happened"}
}))
.Handle("/", h)
http
if err := http.ListenAndServe(":8080", nil); err != nil {
.Fatal().Err(err).Msg("Startup failed")
log}
Multiple Log Output
zerolog.MultiLevelWriter
may be used to send the log
message to multiple outputs. In this example, we send the log message to
both os.Stdout
and the in-built ConsoleWriter.
func main() {
:= zerolog.ConsoleWriter{Out: os.Stdout}
consoleWriter
:= zerolog.MultiLevelWriter(consoleWriter, os.Stdout)
multi
:= zerolog.New(multi).With().Timestamp().Logger()
logger
.Info().Msg("Hello World!")
logger}
// Output (Line 1: Console; Line 2: Stdout)
// 12:36PM INF Hello World!
// {"level":"info","time":"2019-11-07T12:36:38+03:00","message":"Hello World!"}
Global Settings
Some settings can be changed and will be applied to all loggers:
log.Logger
: You can set this value to customize the global logger (the one used by package level methods).zerolog.SetGlobalLevel
: Can raise the minimum level of all loggers. Call this withzerolog.Disabled
to disable logging altogether (quiet mode).zerolog.DisableSampling
: If argument istrue
, all sampled loggers will stop sampling and issue 100% of their log events.zerolog.TimestampFieldName
: Can be set to customizeTimestamp
field name.zerolog.LevelFieldName
: Can be set to customize level field name.zerolog.MessageFieldName
: Can be set to customize message field name.zerolog.ErrorFieldName
: Can be set to customizeErr
field name.zerolog.TimeFieldFormat
: Can be set to customizeTime
field value formatting. If set withzerolog.TimeFormatUnix
,zerolog.TimeFormatUnixMs
orzerolog.TimeFormatUnixMicro
, times are formated as UNIX timestamp.zerolog.DurationFieldUnit
: Can be set to customize the unit for time.Duration type fields added byDur
(default:time.Millisecond
).zerolog.DurationFieldInteger
: If set totrue
,Dur
fields are formatted as integers instead of floats (default:false
).zerolog.ErrorHandler
: Called whenever zerolog fails to write an event on its output. If not set, an error is printed on the stderr. This handler must be thread safe and non-blocking.
Field Types
Standard Types
Str
Bool
Int
,Int8
,Int16
,Int32
,Int64
Uint
,Uint8
,Uint16
,Uint32
,Uint64
Float32
,Float64
Advanced Fields
Err
: Takes anerror
and renders it as a string using thezerolog.ErrorFieldName
field name.Func
: Run afunc
only if the level is enabled.Timestamp
: Inserts a timestamp field withzerolog.TimestampFieldName
field name, formatted usingzerolog.TimeFieldFormat
.Time
: Adds a field with time formatted withzerolog.TimeFieldFormat
.Dur
: Adds a field withtime.Duration
.Dict
: Adds a sub-key/value as a field of the event.RawJSON
: Adds a field with an already encoded JSON ([]byte
)Hex
: Adds a field with value formatted as a hexadecimal string ([]byte
)Interface
: Uses reflection to marshal the type.
Most fields are also available in the slice format (Strs
for []string
, Errs
for []error
etc.)
Binary Encoding
In addition to the default JSON encoding, zerolog
can
produce binary logs using CBOR encoding.
The choice of encoding can be decided at compile time using the build
tag binary_log
as follows:
go build -tags binary_log .
To Decode binary encoded log files you can use any CBOR decoder. One has been tested to work with zerolog library is CSD.
Related Projects
- grpc-zerolog:
Implementation of
grpclog.LoggerV2
interface usingzerolog
- overlog:
Implementation of
Mapped Diagnostic Context
interface usingzerolog
- zerologr:
Implementation of
logr.LogSink
interface usingzerolog
Benchmarks
See logbench for more comprehensive and up-to-date benchmarks.
All operations are allocation free (those numbers include JSON encoding):
BenchmarkLogEmpty-8 100000000 19.1 ns/op 0 B/op 0 allocs/op
BenchmarkDisabled-8 500000000 4.07 ns/op 0 B/op 0 allocs/op
BenchmarkInfo-8 30000000 42.5 ns/op 0 B/op 0 allocs/op
BenchmarkContextFields-8 30000000 44.9 ns/op 0 B/op 0 allocs/op
BenchmarkLogFields-8 10000000 184 ns/op 0 B/op 0 allocs/op
There are a few Go logging benchmarks and comparisons that include zerolog.
Using Uber’s zap comparison benchmark:
Log a message and 10 fields:
Library | Time | Bytes Allocated | Objects Allocated |
---|---|---|---|
zerolog | 767 ns/op | 552 B/op | 6 allocs/op |
⚡ zap | 848 ns/op | 704 B/op | 2 allocs/op |
⚡ zap (sugared) | 1363 ns/op | 1610 B/op | 20 allocs/op |
go-kit | 3614 ns/op | 2895 B/op | 66 allocs/op |
lion | 5392 ns/op | 5807 B/op | 63 allocs/op |
logrus | 5661 ns/op | 6092 B/op | 78 allocs/op |
apex/log | 15332 ns/op | 3832 B/op | 65 allocs/op |
log15 | 20657 ns/op | 5632 B/op | 93 allocs/op |
Log a message with a logger that already has 10 fields of context:
Library | Time | Bytes Allocated | Objects Allocated |
---|---|---|---|
zerolog | 52 ns/op | 0 B/op | 0 allocs/op |
⚡ zap | 283 ns/op | 0 B/op | 0 allocs/op |
⚡ zap (sugared) | 337 ns/op | 80 B/op | 2 allocs/op |
lion | 2702 ns/op | 4074 B/op | 38 allocs/op |
go-kit | 3378 ns/op | 3046 B/op | 52 allocs/op |
logrus | 4309 ns/op | 4564 B/op | 63 allocs/op |
apex/log | 13456 ns/op | 2898 B/op | 51 allocs/op |
log15 | 14179 ns/op | 2642 B/op | 44 allocs/op |
Log a static string, without any context or printf
-style
templating:
Library | Time | Bytes Allocated | Objects Allocated |
---|---|---|---|
zerolog | 50 ns/op | 0 B/op | 0 allocs/op |
⚡ zap | 236 ns/op | 0 B/op | 0 allocs/op |
standard library | 453 ns/op | 80 B/op | 2 allocs/op |
⚡ zap (sugared) | 337 ns/op | 80 B/op | 2 allocs/op |
go-kit | 508 ns/op | 656 B/op | 13 allocs/op |
lion | 771 ns/op | 1224 B/op | 10 allocs/op |
logrus | 1244 ns/op | 1505 B/op | 27 allocs/op |
apex/log | 2751 ns/op | 584 B/op | 11 allocs/op |
log15 | 5181 ns/op | 1592 B/op | 26 allocs/op |
Caveats
Field duplication
Note that zerolog does no de-duplication of fields. Using the same key multiple times creates multiple keys in final JSON:
:= zerolog.New(os.Stderr).With().Timestamp().Logger()
logger .Info().
logger().
Timestamp("dup")
Msg// Output: {"level":"info","time":1494567715,"time":1494567715,"message":"dup"}
In this case, many consumers will take the last value, but this is not guaranteed; check yours if in doubt.
Concurrency safety
Be careful when calling UpdateContext. It is not concurrency safe. Use the With method to create a child logger:
func handler(w http.ResponseWriter, r *http.Request) {
// Create a child logger for concurrency safety
:= log.Logger.With().Logger()
logger
// Add context fields, for example User-Agent from HTTP headers
.UpdateContext(func(c zerolog.Context) zerolog.Context {
logger...
})
}