config.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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
  21. import (
  22. "sort"
  23. "time"
  24. "go.uber.org/zap/zapcore"
  25. )
  26. // SamplingConfig sets a sampling strategy for the logger. Sampling caps the
  27. // global CPU and I/O load that logging puts on your process while attempting
  28. // to preserve a representative subset of your logs.
  29. //
  30. // Values configured here are per-second. See zapcore.NewSampler for details.
  31. type SamplingConfig struct {
  32. Initial int `json:"initial" yaml:"initial"`
  33. Thereafter int `json:"thereafter" yaml:"thereafter"`
  34. }
  35. // Config offers a declarative way to construct a logger. It doesn't do
  36. // anything that can't be done with New, Options, and the various
  37. // zapcore.WriteSyncer and zapcore.Core wrappers, but it's a simpler way to
  38. // toggle common options.
  39. //
  40. // Note that Config intentionally supports only the most common options. More
  41. // unusual logging setups (logging to network connections or message queues,
  42. // splitting output between multiple files, etc.) are possible, but require
  43. // direct use of the zapcore package. For sample code, see the package-level
  44. // BasicConfiguration and AdvancedConfiguration examples.
  45. //
  46. // For an example showing runtime log level changes, see the documentation for
  47. // AtomicLevel.
  48. type Config struct {
  49. // Level is the minimum enabled logging level. Note that this is a dynamic
  50. // level, so calling Config.Level.SetLevel will atomically change the log
  51. // level of all loggers descended from this config.
  52. Level AtomicLevel `json:"level" yaml:"level"`
  53. // Development puts the logger in development mode, which changes the
  54. // behavior of DPanicLevel and takes stacktraces more liberally.
  55. Development bool `json:"development" yaml:"development"`
  56. // DisableCaller stops annotating logs with the calling function's file
  57. // name and line number. By default, all logs are annotated.
  58. DisableCaller bool `json:"disableCaller" yaml:"disableCaller"`
  59. // DisableStacktrace completely disables automatic stacktrace capturing. By
  60. // default, stacktraces are captured for WarnLevel and above logs in
  61. // development and ErrorLevel and above in production.
  62. DisableStacktrace bool `json:"disableStacktrace" yaml:"disableStacktrace"`
  63. // Sampling sets a sampling policy. A nil SamplingConfig disables sampling.
  64. Sampling *SamplingConfig `json:"sampling" yaml:"sampling"`
  65. // Encoding sets the logger's encoding. Valid values are "json" and
  66. // "console", as well as any third-party encodings registered via
  67. // RegisterEncoder.
  68. Encoding string `json:"encoding" yaml:"encoding"`
  69. // EncoderConfig sets options for the chosen encoder. See
  70. // zapcore.EncoderConfig for details.
  71. EncoderConfig zapcore.EncoderConfig `json:"encoderConfig" yaml:"encoderConfig"`
  72. // OutputPaths is a list of URLs or file paths to write logging output to.
  73. // See Open for details.
  74. OutputPaths []string `json:"outputPaths" yaml:"outputPaths"`
  75. // ErrorOutputPaths is a list of URLs to write internal logger errors to.
  76. // The default is standard error.
  77. //
  78. // Note that this setting only affects internal errors; for sample code that
  79. // sends error-level logs to a different location from info- and debug-level
  80. // logs, see the package-level AdvancedConfiguration example.
  81. ErrorOutputPaths []string `json:"errorOutputPaths" yaml:"errorOutputPaths"`
  82. // InitialFields is a collection of fields to add to the root logger.
  83. InitialFields map[string]interface{} `json:"initialFields" yaml:"initialFields"`
  84. }
  85. // NewProductionEncoderConfig returns an opinionated EncoderConfig for
  86. // production environments.
  87. func NewProductionEncoderConfig() zapcore.EncoderConfig {
  88. return zapcore.EncoderConfig{
  89. TimeKey: "ts",
  90. LevelKey: "level",
  91. NameKey: "logger",
  92. CallerKey: "caller",
  93. MessageKey: "msg",
  94. StacktraceKey: "stacktrace",
  95. LineEnding: zapcore.DefaultLineEnding,
  96. EncodeLevel: zapcore.LowercaseLevelEncoder,
  97. EncodeTime: zapcore.EpochTimeEncoder,
  98. EncodeDuration: zapcore.SecondsDurationEncoder,
  99. EncodeCaller: zapcore.ShortCallerEncoder,
  100. }
  101. }
  102. // NewProductionConfig is a reasonable production logging configuration.
  103. // Logging is enabled at InfoLevel and above.
  104. //
  105. // It uses a JSON encoder, writes to standard error, and enables sampling.
  106. // Stacktraces are automatically included on logs of ErrorLevel and above.
  107. func NewProductionConfig() Config {
  108. return Config{
  109. Level: NewAtomicLevelAt(InfoLevel),
  110. Development: false,
  111. Sampling: &SamplingConfig{
  112. Initial: 100,
  113. Thereafter: 100,
  114. },
  115. Encoding: "json",
  116. EncoderConfig: NewProductionEncoderConfig(),
  117. OutputPaths: []string{"stderr"},
  118. ErrorOutputPaths: []string{"stderr"},
  119. }
  120. }
  121. // NewDevelopmentEncoderConfig returns an opinionated EncoderConfig for
  122. // development environments.
  123. func NewDevelopmentEncoderConfig() zapcore.EncoderConfig {
  124. return zapcore.EncoderConfig{
  125. // Keys can be anything except the empty string.
  126. TimeKey: "T",
  127. LevelKey: "L",
  128. NameKey: "N",
  129. CallerKey: "C",
  130. MessageKey: "M",
  131. StacktraceKey: "S",
  132. LineEnding: zapcore.DefaultLineEnding,
  133. EncodeLevel: zapcore.CapitalLevelEncoder,
  134. EncodeTime: zapcore.ISO8601TimeEncoder,
  135. EncodeDuration: zapcore.StringDurationEncoder,
  136. EncodeCaller: zapcore.ShortCallerEncoder,
  137. }
  138. }
  139. // NewDevelopmentConfig is a reasonable development logging configuration.
  140. // Logging is enabled at DebugLevel and above.
  141. //
  142. // It enables development mode (which makes DPanicLevel logs panic), uses a
  143. // console encoder, writes to standard error, and disables sampling.
  144. // Stacktraces are automatically included on logs of WarnLevel and above.
  145. func NewDevelopmentConfig() Config {
  146. return Config{
  147. Level: NewAtomicLevelAt(DebugLevel),
  148. Development: true,
  149. Encoding: "console",
  150. EncoderConfig: NewDevelopmentEncoderConfig(),
  151. OutputPaths: []string{"stderr"},
  152. ErrorOutputPaths: []string{"stderr"},
  153. }
  154. }
  155. // Build constructs a logger from the Config and Options.
  156. func (cfg Config) Build(opts ...Option) (*Logger, error) {
  157. enc, err := cfg.buildEncoder()
  158. if err != nil {
  159. return nil, err
  160. }
  161. sink, errSink, err := cfg.openSinks()
  162. if err != nil {
  163. return nil, err
  164. }
  165. log := New(
  166. zapcore.NewCore(enc, sink, cfg.Level),
  167. cfg.buildOptions(errSink)...,
  168. )
  169. if len(opts) > 0 {
  170. log = log.WithOptions(opts...)
  171. }
  172. return log, nil
  173. }
  174. func (cfg Config) buildOptions(errSink zapcore.WriteSyncer) []Option {
  175. opts := []Option{ErrorOutput(errSink)}
  176. if cfg.Development {
  177. opts = append(opts, Development())
  178. }
  179. if !cfg.DisableCaller {
  180. opts = append(opts, AddCaller())
  181. }
  182. stackLevel := ErrorLevel
  183. if cfg.Development {
  184. stackLevel = WarnLevel
  185. }
  186. if !cfg.DisableStacktrace {
  187. opts = append(opts, AddStacktrace(stackLevel))
  188. }
  189. if cfg.Sampling != nil {
  190. opts = append(opts, WrapCore(func(core zapcore.Core) zapcore.Core {
  191. return zapcore.NewSampler(core, time.Second, int(cfg.Sampling.Initial), int(cfg.Sampling.Thereafter))
  192. }))
  193. }
  194. if len(cfg.InitialFields) > 0 {
  195. fs := make([]Field, 0, len(cfg.InitialFields))
  196. keys := make([]string, 0, len(cfg.InitialFields))
  197. for k := range cfg.InitialFields {
  198. keys = append(keys, k)
  199. }
  200. sort.Strings(keys)
  201. for _, k := range keys {
  202. fs = append(fs, Any(k, cfg.InitialFields[k]))
  203. }
  204. opts = append(opts, Fields(fs...))
  205. }
  206. return opts
  207. }
  208. func (cfg Config) openSinks() (zapcore.WriteSyncer, zapcore.WriteSyncer, error) {
  209. sink, closeOut, err := Open(cfg.OutputPaths...)
  210. if err != nil {
  211. return nil, nil, err
  212. }
  213. errSink, _, err := Open(cfg.ErrorOutputPaths...)
  214. if err != nil {
  215. closeOut()
  216. return nil, nil, err
  217. }
  218. return sink, errSink, nil
  219. }
  220. func (cfg Config) buildEncoder() (zapcore.Encoder, error) {
  221. return newEncoder(cfg.Encoding, cfg.EncoderConfig)
  222. }