Categorygithub.com/rs/zap
repositorypackage
1.4.0
Repository: https://github.com/rs/zap.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

:zap: zap GoDoc Build Status Coverage Status

Blazing fast, structured, leveled logging in Go.

Installation

go get -u go.uber.org/zap

Note that zap only supports the two most recent minor versions of Go.

Quick Start

In contexts where performance is nice, but not critical, use the SugaredLogger. It's 4-10x faster than than other structured logging libraries and includes both structured and printf-style APIs.

logger, _ := zap.NewProduction()
defer logger.Sync() // flushes buffer, if any
sugar := logger.Sugar()
sugar.Infow("Failed to fetch URL.",
  // Structured context as loosely-typed key-value pairs.
  "url", url,
  "attempt", 3,
  "backoff", time.Second,
)
sugar.Infof("Failed to fetch URL: %s", url)

When performance and type safety are critical, use the Logger. It's even faster than the SugaredLogger and allocates far less, but it only supports structured logging.

logger, _ := zap.NewProduction()
defer logger.Sync()
logger.Info("Failed to fetch URL.",
  // Structured context as strongly-typed Field values.
  zap.String("url", url),
  zap.Int("attempt", 3),
  zap.Duration("backoff", time.Second),
)

Performance

For applications that log in the hot path, reflection-based serialization and string formatting are prohibitively expensive — they're CPU-intensive and make many small allocations. Put differently, using encoding/json and fmt.Fprintf to log tons of interface{}s makes your application slow.

Zap takes a different approach. It includes a reflection-free, zero-allocation JSON encoder, and the base Logger strives to avoid serialization overhead and allocations wherever possible. By building the high-level SugaredLogger on that foundation, zap lets users choose when they need to count every allocation and when they'd prefer a more familiar, loosely-typed API.

As measured by its own benchmarking suite, not only is zap more performant than comparable structured logging libraries — it's also faster than the standard library. Like all benchmarks, take these with a grain of salt.1

Log a message and 10 fields:

LibraryTimeBytes AllocatedObjects Allocated
:zap: zap1526 ns/op704 B/op2 allocs/op
:zap: zap (sugared)2274 ns/op1610 B/op20 allocs/op
go-kit5854 ns/op2895 B/op66 allocs/op
logrus9117 ns/op6092 B/op78 allocs/op
lion9408 ns/op5807 B/op63 allocs/op
apex/log17007 ns/op3832 B/op65 allocs/op
log1521290 ns/op5632 B/op93 allocs/op

Log a message with a logger that already has 10 fields of context:

LibraryTimeBytes AllocatedObjects Allocated
:zap: zap446 ns/op0 B/op0 allocs/op
:zap: zap (sugared)599 ns/op80 B/op2 allocs/op
lion5231 ns/op4074 B/op38 allocs/op
go-kit6424 ns/op3046 B/op52 allocs/op
logrus7578 ns/op4564 B/op63 allocs/op
apex/log15697 ns/op2898 B/op51 allocs/op
log1515879 ns/op2642 B/op44 allocs/op

Log a static string, without any context or printf-style templating:

LibraryTimeBytes AllocatedObjects Allocated
:zap: zap418 ns/op0 B/op0 allocs/op
standard library524 ns/op80 B/op2 allocs/op
:zap: zap (sugared)628 ns/op80 B/op2 allocs/op
go-kit1011 ns/op656 B/op13 allocs/op
lion1382 ns/op1224 B/op10 allocs/op
logrus2263 ns/op1505 B/op27 allocs/op
apex/log3198 ns/op584 B/op11 allocs/op
log155787 ns/op1592 B/op26 allocs/op

Development Status: Stable

All APIs are finalized, and no breaking changes will be made in the 1.x series of releases. Users of semver-aware dependency management systems should pin zap to ^1.


Released under the MIT License.

1 In particular, keep in mind that we may be benchmarking against slightly older versions of other libraries. Versions are pinned in zap's glide.lock file.