.readme.tmpl 3.8 KB

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