Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 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 | |
| 21 | package zap |
| 22 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame^] | 23 | import ( |
| 24 | "fmt" |
| 25 | |
| 26 | "go.uber.org/zap/zapcore" |
| 27 | ) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 28 | |
| 29 | // An Option configures a Logger. |
| 30 | type Option interface { |
| 31 | apply(*Logger) |
| 32 | } |
| 33 | |
| 34 | // optionFunc wraps a func so it satisfies the Option interface. |
| 35 | type optionFunc func(*Logger) |
| 36 | |
| 37 | func (f optionFunc) apply(log *Logger) { |
| 38 | f(log) |
| 39 | } |
| 40 | |
| 41 | // WrapCore wraps or replaces the Logger's underlying zapcore.Core. |
| 42 | func WrapCore(f func(zapcore.Core) zapcore.Core) Option { |
| 43 | return optionFunc(func(log *Logger) { |
| 44 | log.core = f(log.core) |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | // Hooks registers functions which will be called each time the Logger writes |
| 49 | // out an Entry. Repeated use of Hooks is additive. |
| 50 | // |
| 51 | // Hooks are useful for simple side effects, like capturing metrics for the |
| 52 | // number of emitted logs. More complex side effects, including anything that |
| 53 | // requires access to the Entry's structured fields, should be implemented as |
| 54 | // a zapcore.Core instead. See zapcore.RegisterHooks for details. |
| 55 | func Hooks(hooks ...func(zapcore.Entry) error) Option { |
| 56 | return optionFunc(func(log *Logger) { |
| 57 | log.core = zapcore.RegisterHooks(log.core, hooks...) |
| 58 | }) |
| 59 | } |
| 60 | |
| 61 | // Fields adds fields to the Logger. |
| 62 | func Fields(fs ...Field) Option { |
| 63 | return optionFunc(func(log *Logger) { |
| 64 | log.core = log.core.With(fs) |
| 65 | }) |
| 66 | } |
| 67 | |
| 68 | // ErrorOutput sets the destination for errors generated by the Logger. Note |
| 69 | // that this option only affects internal errors; for sample code that sends |
| 70 | // error-level logs to a different location from info- and debug-level logs, |
| 71 | // see the package-level AdvancedConfiguration example. |
| 72 | // |
| 73 | // The supplied WriteSyncer must be safe for concurrent use. The Open and |
| 74 | // zapcore.Lock functions are the simplest ways to protect files with a mutex. |
| 75 | func ErrorOutput(w zapcore.WriteSyncer) Option { |
| 76 | return optionFunc(func(log *Logger) { |
| 77 | log.errorOutput = w |
| 78 | }) |
| 79 | } |
| 80 | |
| 81 | // Development puts the logger in development mode, which makes DPanic-level |
| 82 | // logs panic instead of simply logging an error. |
| 83 | func Development() Option { |
| 84 | return optionFunc(func(log *Logger) { |
| 85 | log.development = true |
| 86 | }) |
| 87 | } |
| 88 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame^] | 89 | // AddCaller configures the Logger to annotate each message with the filename, |
| 90 | // line number, and function name of zap's caller. See also WithCaller. |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 91 | func AddCaller() Option { |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame^] | 92 | return WithCaller(true) |
| 93 | } |
| 94 | |
| 95 | // WithCaller configures the Logger to annotate each message with the filename, |
| 96 | // line number, and function name of zap's caller, or not, depending on the |
| 97 | // value of enabled. This is a generalized form of AddCaller. |
| 98 | func WithCaller(enabled bool) Option { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 99 | return optionFunc(func(log *Logger) { |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame^] | 100 | log.addCaller = enabled |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 101 | }) |
| 102 | } |
| 103 | |
| 104 | // AddCallerSkip increases the number of callers skipped by caller annotation |
| 105 | // (as enabled by the AddCaller option). When building wrappers around the |
| 106 | // Logger and SugaredLogger, supplying this Option prevents zap from always |
| 107 | // reporting the wrapper code as the caller. |
| 108 | func AddCallerSkip(skip int) Option { |
| 109 | return optionFunc(func(log *Logger) { |
| 110 | log.callerSkip += skip |
| 111 | }) |
| 112 | } |
| 113 | |
| 114 | // AddStacktrace configures the Logger to record a stack trace for all messages at |
| 115 | // or above a given level. |
| 116 | func AddStacktrace(lvl zapcore.LevelEnabler) Option { |
| 117 | return optionFunc(func(log *Logger) { |
| 118 | log.addStack = lvl |
| 119 | }) |
| 120 | } |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame^] | 121 | |
| 122 | // IncreaseLevel increase the level of the logger. It has no effect if |
| 123 | // the passed in level tries to decrease the level of the logger. |
| 124 | func IncreaseLevel(lvl zapcore.LevelEnabler) Option { |
| 125 | return optionFunc(func(log *Logger) { |
| 126 | core, err := zapcore.NewIncreaseLevelCore(log.core, lvl) |
| 127 | if err != nil { |
| 128 | fmt.Fprintf(log.errorOutput, "failed to IncreaseLevel: %v\n", err) |
| 129 | } else { |
| 130 | log.core = core |
| 131 | } |
| 132 | }) |
| 133 | } |
| 134 | |
| 135 | // OnFatal sets the action to take on fatal logs. |
| 136 | func OnFatal(action zapcore.CheckWriteAction) Option { |
| 137 | return optionFunc(func(log *Logger) { |
| 138 | log.onFatal = action |
| 139 | }) |
| 140 | } |
| 141 | |
| 142 | // WithClock specifies the clock used by the logger to determine the current |
| 143 | // time for logged entries. Defaults to the system clock with time.Now. |
| 144 | func WithClock(clock zapcore.Clock) Option { |
| 145 | return optionFunc(func(log *Logger) { |
| 146 | log.clock = clock |
| 147 | }) |
| 148 | } |