• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

benchmarks/H30-Apr-2019-

buffer/H30-Apr-2019-

internal/H30-Apr-2019-

scripts/H30-Apr-2019-

zapcore/H30-Apr-2019-

zapgrpc/H30-Apr-2019-

zaptest/H30-Apr-2019-

.codecov.ymlH A D30-Apr-2019801

.gitignoreH A D30-Apr-2019293

.readme.tmplH A D30-Apr-20193.8 KiB

.travis.ymlH A D30-Apr-2019317

CHANGELOG.mdH A D30-Apr-201911.6 KiB

CODE_OF_CONDUCT.mdH A D30-Apr-20193.1 KiB

CONTRIBUTING.mdH A D30-Apr-20192.4 KiB

FAQ.mdH A D30-Apr-20196 KiB

MakefileH A D30-Apr-20192.2 KiB

README.mdH A D30-Apr-20195 KiB

array.goH A D30-Apr-20197.8 KiB

array_test.goH A D30-Apr-20195 KiB

check_license.shH A D30-Apr-2019294

common_test.goH A D30-Apr-20192 KiB

config.goH A D30-Apr-20198.6 KiB

config_test.goH A D30-Apr-20193.8 KiB

doc.goH A D30-Apr-20195.1 KiB

encoder.goH A D30-Apr-20192.7 KiB

encoder_test.goH A D30-Apr-20193.3 KiB

error.goH A D30-Apr-20192.7 KiB

error_test.goH A D30-Apr-20193.7 KiB

example_test.goH A D30-Apr-20199.8 KiB

field.goH A D30-Apr-201910.3 KiB

field_test.goH A D30-Apr-20197.8 KiB

flag.goH A D30-Apr-20191.6 KiB

flag_test.goH A D30-Apr-20193.2 KiB

glide.lockH A D30-Apr-20192.5 KiB

glide.yamlH A D30-Apr-2019804

global.goH A D30-Apr-20195.2 KiB

global_go112.goH A D30-Apr-20191.2 KiB

global_prego112.goH A D30-Apr-20191.2 KiB

global_test.goH A D30-Apr-20199.2 KiB

http_handler.goH A D30-Apr-20192.4 KiB

http_handler_test.goH A D30-Apr-20194.2 KiB

level.goH A D30-Apr-20194.6 KiB

level_test.goH A D30-Apr-20193.7 KiB

logger.goH A D30-Apr-20199.8 KiB

logger_bench_test.goH A D30-Apr-20195.4 KiB

logger_test.goH A D30-Apr-201913.5 KiB

options.goH A D30-Apr-20193.8 KiB

sink.goH A D30-Apr-20194.5 KiB

sink_test.goH A D30-Apr-20193.2 KiB

stacktrace.goH A D30-Apr-20193.8 KiB

stacktrace_ext_test.goH A D30-Apr-20196 KiB

stacktrace_test.goH A D30-Apr-20192.5 KiB

sugar.goH A D30-Apr-201910.2 KiB

sugar_test.goH A D30-Apr-201913 KiB

time.goH A D30-Apr-20191.2 KiB

time_test.goH A D30-Apr-20191.5 KiB

writer.goH A D30-Apr-20193.5 KiB

writer_test.goH A D30-Apr-20195.7 KiB

README.md

1# :zap: zap [![GoDoc][doc-img]][doc] [![Build Status][ci-img]][ci] [![Coverage Status][cov-img]][cov]
2
3Blazing fast, structured, leveled logging in Go.
4
5## Installation
6
7`go get -u go.uber.org/zap`
8
9Note that zap only supports the two most recent minor versions of Go.
10
11## Quick Start
12
13In contexts where performance is nice, but not critical, use the
14`SugaredLogger`. It's 4-10x faster than other structured logging
15packages and includes both structured and `printf`-style APIs.
16
17```go
18logger, _ := zap.NewProduction()
19defer logger.Sync() // flushes buffer, if any
20sugar := logger.Sugar()
21sugar.Infow("failed to fetch URL",
22  // Structured context as loosely typed key-value pairs.
23  "url", url,
24  "attempt", 3,
25  "backoff", time.Second,
26)
27sugar.Infof("Failed to fetch URL: %s", url)
28```
29
30When performance and type safety are critical, use the `Logger`. It's even
31faster than the `SugaredLogger` and allocates far less, but it only supports
32structured logging.
33
34```go
35logger, _ := zap.NewProduction()
36defer logger.Sync()
37logger.Info("failed to fetch URL",
38  // Structured context as strongly typed Field values.
39  zap.String("url", url),
40  zap.Int("attempt", 3),
41  zap.Duration("backoff", time.Second),
42)
43```
44
45See the [documentation][doc] and [FAQ](FAQ.md) for more details.
46
47## Performance
48
49For applications that log in the hot path, reflection-based serialization and
50string formatting are prohibitively expensive — they're CPU-intensive
51and make many small allocations. Put differently, using `encoding/json` and
52`fmt.Fprintf` to log tons of `interface{}`s makes your application slow.
53
54Zap takes a different approach. It includes a reflection-free, zero-allocation
55JSON encoder, and the base `Logger` strives to avoid serialization overhead
56and allocations wherever possible. By building the high-level `SugaredLogger`
57on that foundation, zap lets users *choose* when they need to count every
58allocation and when they'd prefer a more familiar, loosely typed API.
59
60As measured by its own [benchmarking suite][], not only is zap more performant
61than comparable structured logging packages — it's also faster than the
62standard library. Like all benchmarks, take these with a grain of salt.<sup
63id="anchor-versions">[1](#footnote-versions)</sup>
64
65Log a message and 10 fields:
66
67| Package | Time | Objects Allocated |
68| :--- | :---: | :---: |
69| :zap: zap | 3131 ns/op | 5 allocs/op |
70| :zap: zap (sugared) | 4173 ns/op | 21 allocs/op |
71| zerolog | 16154 ns/op | 90 allocs/op |
72| lion | 16341 ns/op | 111 allocs/op |
73| go-kit | 17049 ns/op | 126 allocs/op |
74| logrus | 23662 ns/op | 142 allocs/op |
75| log15 | 36351 ns/op | 149 allocs/op |
76| apex/log | 42530 ns/op | 126 allocs/op |
77
78Log a message with a logger that already has 10 fields of context:
79
80| Package | Time | Objects Allocated |
81| :--- | :---: | :---: |
82| :zap: zap | 380 ns/op | 0 allocs/op |
83| :zap: zap (sugared) | 564 ns/op | 2 allocs/op |
84| zerolog | 321 ns/op | 0 allocs/op |
85| lion | 7092 ns/op | 39 allocs/op |
86| go-kit | 20226 ns/op | 115 allocs/op |
87| logrus | 22312 ns/op | 130 allocs/op |
88| log15 | 28788 ns/op | 79 allocs/op |
89| apex/log | 42063 ns/op | 115 allocs/op |
90
91Log a static string, without any context or `printf`-style templating:
92
93| Package | Time | Objects Allocated |
94| :--- | :---: | :---: |
95| :zap: zap | 361 ns/op | 0 allocs/op |
96| :zap: zap (sugared) | 534 ns/op | 2 allocs/op |
97| zerolog | 323 ns/op | 0 allocs/op |
98| standard library | 575 ns/op | 2 allocs/op |
99| go-kit | 922 ns/op | 13 allocs/op |
100| lion | 1413 ns/op | 10 allocs/op |
101| logrus | 2291 ns/op | 27 allocs/op |
102| apex/log | 3690 ns/op | 11 allocs/op |
103| log15 | 5954 ns/op | 26 allocs/op |
104
105## Development Status: Stable
106
107All APIs are finalized, and no breaking changes will be made in the 1.x series
108of releases. Users of semver-aware dependency management systems should pin
109zap to `^1`.
110
111## Contributing
112
113We encourage and support an active, healthy community of contributors &mdash;
114including you! Details are in the [contribution guide](CONTRIBUTING.md) and
115the [code of conduct](CODE_OF_CONDUCT.md). The zap maintainers keep an eye on
116issues and pull requests, but you can also report any negative conduct to
117oss-conduct@uber.com. That email list is a private, safe space; even the zap
118maintainers don't have access, so don't hesitate to hold us to a high
119standard.
120
121<hr>
122
123Released under the [MIT License](LICENSE.txt).
124
125<sup id="footnote-versions">1</sup> In particular, keep in mind that we may be
126benchmarking against slightly older versions of other packages. Versions are
127pinned in zap's [glide.lock][] file. [↩](#anchor-versions)
128
129[doc-img]: https://godoc.org/go.uber.org/zap?status.svg
130[doc]: https://godoc.org/go.uber.org/zap
131[ci-img]: https://travis-ci.org/uber-go/zap.svg?branch=master
132[ci]: https://travis-ci.org/uber-go/zap
133[cov-img]: https://codecov.io/gh/uber-go/zap/branch/master/graph/badge.svg
134[cov]: https://codecov.io/gh/uber-go/zap
135[benchmarking suite]: https://github.com/uber-go/zap/tree/master/benchmarks
136[glide.lock]: https://github.com/uber-go/zap/blob/master/glide.lock
137