Categorygithub.com/gxlz/zap
repositorypackage
1.0.0
Repository: https://github.com/gxlz/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

# README

:zap: zap GoDoc Build Status Coverage Status

Blazing fast, structured, leveled logging in Go.

Installation

go get -u go.uber.org/zap

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()
sugar := logger.Sugar()
sugar.Infow("Failed to fetch URL.",
  // Structured context as loosely-typed key-value pairs.
  "url", url,
  "attempt", retryNum,
  "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()
logger.Info("Failed to fetch URL.",
  // Structured context as strongly-typed Field values.
  zap.String("url", url),
  zap.Int("attempt", tryNum),
  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: zap1466 ns/op705 B/op2 allocs/op
:zap: zap (sugared)2893 ns/op1931 B/op21 allocs/op
go-kit8183 ns/op3119 B/op65 allocs/op
lion12259 ns/op5999 B/op62 allocs/op
logrus12862 ns/op5783 B/op77 allocs/op
apex/log20317 ns/op4024 B/op64 allocs/op
log1531855 ns/op5536 B/op91 allocs/op

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

LibraryTimeBytes AllocatedObjects Allocated
:zap: zap536 ns/op0 B/op0 allocs/op
:zap: zap (sugared)734 ns/op80 B/op2 allocs/op
lion6784 ns/op3978 B/op36 allocs/op
go-kit8316 ns/op2950 B/op50 allocs/op
logrus10160 ns/op3967 B/op61 allocs/op
apex/log17095 ns/op2801 B/op49 allocs/op
log1519112 ns/op2545 B/op42 allocs/op

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

LibraryTimeBytes AllocatedObjects Allocated
:zap: zap521 ns/op0 B/op0 allocs/op
standard library580 ns/op80 B/op2 allocs/op
:zap: zap (sugared)885 ns/op80 B/op2 allocs/op
go-kit1384 ns/op656 B/op13 allocs/op
lion2009 ns/op1224 B/op10 allocs/op
logrus2925 ns/op1409 B/op25 allocs/op
apex/log3723 ns/op584 B/op11 allocs/op
log156349 ns/op1496 B/op24 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.