doc.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. // Package zap provides fast, structured, leveled logging.
  21. //
  22. // For applications that log in the hot path, reflection-based serialization
  23. // and string formatting are prohibitively expensive - they're CPU-intensive
  24. // and make many small allocations. Put differently, using json.Marshal and
  25. // fmt.Fprintf to log tons of interface{} makes your application slow.
  26. //
  27. // Zap takes a different approach. It includes a reflection-free,
  28. // zero-allocation JSON encoder, and the base Logger strives to avoid
  29. // serialization overhead and allocations wherever possible. By building the
  30. // high-level SugaredLogger on that foundation, zap lets users choose when
  31. // they need to count every allocation and when they'd prefer a more familiar,
  32. // loosely typed API.
  33. //
  34. // Choosing a Logger
  35. //
  36. // In contexts where performance is nice, but not critical, use the
  37. // SugaredLogger. It's 4-10x faster than other structured logging packages and
  38. // supports both structured and printf-style logging. Like log15 and go-kit,
  39. // the SugaredLogger's structured logging APIs are loosely typed and accept a
  40. // variadic number of key-value pairs. (For more advanced use cases, they also
  41. // accept strongly typed fields - see the SugaredLogger.With documentation for
  42. // details.)
  43. // sugar := zap.NewExample().Sugar()
  44. // defer sugar.Sync()
  45. // sugar.Infow("failed to fetch URL",
  46. // "url", "http://example.com",
  47. // "attempt", 3,
  48. // "backoff", time.Second,
  49. // )
  50. // sugar.Infof("failed to fetch URL: %s", "http://example.com")
  51. //
  52. // By default, loggers are unbuffered. However, since zap's low-level APIs
  53. // allow buffering, calling Sync before letting your process exit is a good
  54. // habit.
  55. //
  56. // In the rare contexts where every microsecond and every allocation matter,
  57. // use the Logger. It's even faster than the SugaredLogger and allocates far
  58. // less, but it only supports strongly-typed, structured logging.
  59. // logger := zap.NewExample()
  60. // defer logger.Sync()
  61. // logger.Info("failed to fetch URL",
  62. // zap.String("url", "http://example.com"),
  63. // zap.Int("attempt", 3),
  64. // zap.Duration("backoff", time.Second),
  65. // )
  66. //
  67. // Choosing between the Logger and SugaredLogger doesn't need to be an
  68. // application-wide decision: converting between the two is simple and
  69. // inexpensive.
  70. // logger := zap.NewExample()
  71. // defer logger.Sync()
  72. // sugar := logger.Sugar()
  73. // plain := sugar.Desugar()
  74. //
  75. // Configuring Zap
  76. //
  77. // The simplest way to build a Logger is to use zap's opinionated presets:
  78. // NewExample, NewProduction, and NewDevelopment. These presets build a logger
  79. // with a single function call:
  80. // logger, err := zap.NewProduction()
  81. // if err != nil {
  82. // log.Fatalf("can't initialize zap logger: %v", err)
  83. // }
  84. // defer logger.Sync()
  85. //
  86. // Presets are fine for small projects, but larger projects and organizations
  87. // naturally require a bit more customization. For most users, zap's Config
  88. // struct strikes the right balance between flexibility and convenience. See
  89. // the package-level BasicConfiguration example for sample code.
  90. //
  91. // More unusual configurations (splitting output between files, sending logs
  92. // to a message queue, etc.) are possible, but require direct use of
  93. // go.uber.org/zap/zapcore. See the package-level AdvancedConfiguration
  94. // example for sample code.
  95. //
  96. // Extending Zap
  97. //
  98. // The zap package itself is a relatively thin wrapper around the interfaces
  99. // in go.uber.org/zap/zapcore. Extending zap to support a new encoding (e.g.,
  100. // BSON), a new log sink (e.g., Kafka), or something more exotic (perhaps an
  101. // exception aggregation service, like Sentry or Rollbar) typically requires
  102. // implementing the zapcore.Encoder, zapcore.WriteSyncer, or zapcore.Core
  103. // interfaces. See the zapcore documentation for details.
  104. //
  105. // Similarly, package authors can use the high-performance Encoder and Core
  106. // implementations in the zapcore package to build their own loggers.
  107. //
  108. // Frequently Asked Questions
  109. //
  110. // An FAQ covering everything from installation errors to design decisions is
  111. // available at https://github.com/uber-go/zap/blob/master/FAQ.md.
  112. package zap // import "go.uber.org/zap"