options.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "go.uber.org/zap/zapcore"
  22. // An Option configures a Logger.
  23. type Option interface {
  24. apply(*Logger)
  25. }
  26. // optionFunc wraps a func so it satisfies the Option interface.
  27. type optionFunc func(*Logger)
  28. func (f optionFunc) apply(log *Logger) {
  29. f(log)
  30. }
  31. // WrapCore wraps or replaces the Logger's underlying zapcore.Core.
  32. func WrapCore(f func(zapcore.Core) zapcore.Core) Option {
  33. return optionFunc(func(log *Logger) {
  34. log.core = f(log.core)
  35. })
  36. }
  37. // Hooks registers functions which will be called each time the Logger writes
  38. // out an Entry. Repeated use of Hooks is additive.
  39. //
  40. // Hooks are useful for simple side effects, like capturing metrics for the
  41. // number of emitted logs. More complex side effects, including anything that
  42. // requires access to the Entry's structured fields, should be implemented as
  43. // a zapcore.Core instead. See zapcore.RegisterHooks for details.
  44. func Hooks(hooks ...func(zapcore.Entry) error) Option {
  45. return optionFunc(func(log *Logger) {
  46. log.core = zapcore.RegisterHooks(log.core, hooks...)
  47. })
  48. }
  49. // Fields adds fields to the Logger.
  50. func Fields(fs ...Field) Option {
  51. return optionFunc(func(log *Logger) {
  52. log.core = log.core.With(fs)
  53. })
  54. }
  55. // ErrorOutput sets the destination for errors generated by the Logger. Note
  56. // that this option only affects internal errors; for sample code that sends
  57. // error-level logs to a different location from info- and debug-level logs,
  58. // see the package-level AdvancedConfiguration example.
  59. //
  60. // The supplied WriteSyncer must be safe for concurrent use. The Open and
  61. // zapcore.Lock functions are the simplest ways to protect files with a mutex.
  62. func ErrorOutput(w zapcore.WriteSyncer) Option {
  63. return optionFunc(func(log *Logger) {
  64. log.errorOutput = w
  65. })
  66. }
  67. // Development puts the logger in development mode, which makes DPanic-level
  68. // logs panic instead of simply logging an error.
  69. func Development() Option {
  70. return optionFunc(func(log *Logger) {
  71. log.development = true
  72. })
  73. }
  74. // AddCaller configures the Logger to annotate each message with the filename
  75. // and line number of zap's caller.
  76. func AddCaller() Option {
  77. return optionFunc(func(log *Logger) {
  78. log.addCaller = true
  79. })
  80. }
  81. // AddCallerSkip increases the number of callers skipped by caller annotation
  82. // (as enabled by the AddCaller option). When building wrappers around the
  83. // Logger and SugaredLogger, supplying this Option prevents zap from always
  84. // reporting the wrapper code as the caller.
  85. func AddCallerSkip(skip int) Option {
  86. return optionFunc(func(log *Logger) {
  87. log.callerSkip += skip
  88. })
  89. }
  90. // AddStacktrace configures the Logger to record a stack trace for all messages at
  91. // or above a given level.
  92. func AddStacktrace(lvl zapcore.LevelEnabler) Option {
  93. return optionFunc(func(log *Logger) {
  94. log.addStack = lvl
  95. })
  96. }