| 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 zapscip-go gomod go.uber.org/zap . `go.uber.org/zap`/ |
| 22 | |
| 23 | import ( |
| 24 | "fmtscip-go gomod github.com/golang/go/src go1.19 fmt/" |
| 25 | |
| 26 | "go.uber.org/zap/zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/" |
| 27 | ) |
| 28 | |
| 29 | // An Option configures a Logger. |
| 30 | type Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# interface { |
| 31 | applyscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option#apply.(*Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) |
| 32 | } |
| 33 | |
| 34 | // optionFunc wraps a func so it satisfies the Option interface. |
| 35 | type optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc# func(*Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) |
| 36 | |
| 37 | func (f optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#) applyscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#apply().(log *Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) { |
| 38 | f(log) |
| 39 | } |
| 40 | |
| 41 | // WrapCore wraps or replaces the Logger's underlying zapcore.Core. |
| 42 | func WrapCorescip-go gomod go.uber.org/zap . `go.uber.org/zap`/WrapCore().(f func(zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.Corescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Core#) zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.Corescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Core#) Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 43 | return optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#(func(log *Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) { |
| 44 | log.corescip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#core. = f(log.corescip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#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 Hooksscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Hooks().(hooks ...func(zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.Entryscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Entry#) error) Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 56 | return optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#(func(log *Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) { |
| 57 | log.corescip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#core. = zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.RegisterHooksscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/RegisterHooks().(log.corescip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#core., hooks...) |
| 58 | }) |
| 59 | } |
| 60 | |
| 61 | // Fields adds fields to the Logger. |
| 62 | func Fieldsscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Fields().(fs ...Fieldscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Field#) Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 63 | return optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#(func(log *Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) { |
| 64 | log.corescip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#core. = log.corescip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#core..Withscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/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 ErrorOutputscip-go gomod go.uber.org/zap . `go.uber.org/zap`/ErrorOutput().(w zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.WriteSyncerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/WriteSyncer#) Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 76 | return optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#(func(log *Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) { |
| 77 | log.errorOutputscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#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 Developmentscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Development().() Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 84 | return optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#(func(log *Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) { |
| 85 | log.developmentscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#development. = true |
| 86 | }) |
| 87 | } |
| 88 | |
| 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. |
| 91 | func AddCallerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/AddCaller().() Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 92 | return WithCallerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/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 WithCallerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/WithCaller().(enabled bool) Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 99 | return optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#(func(log *Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) { |
| 100 | log.addCallerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#addCaller. = enabled |
| 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 AddCallerSkipscip-go gomod go.uber.org/zap . `go.uber.org/zap`/AddCallerSkip().(skip int) Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 109 | return optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#(func(log *Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) { |
| 110 | log.callerSkipscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#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 AddStacktracescip-go gomod go.uber.org/zap . `go.uber.org/zap`/AddStacktrace().(lvl zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.LevelEnablerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LevelEnabler#) Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 117 | return optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#(func(log *Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) { |
| 118 | log.addStackscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#addStack. = lvl |
| 119 | }) |
| 120 | } |
| 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 IncreaseLevelscip-go gomod go.uber.org/zap . `go.uber.org/zap`/IncreaseLevel().(lvl zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.LevelEnablerscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/LevelEnabler#) Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 125 | return optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#(func(log *Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) { |
| 126 | core, err := zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.NewIncreaseLevelCorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/NewIncreaseLevelCore().(log.corescip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#core., lvl) |
| 127 | if err != nil { |
| 128 | fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.19 fmt/Fprintf().(log.errorOutputscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#errorOutput., "failed to IncreaseLevel: %v\n", err) |
| 129 | } else { |
| 130 | log.corescip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#core. = core |
| 131 | } |
| 132 | }) |
| 133 | } |
| 134 | |
| 135 | // WithPanicHook sets a CheckWriteHook to run on Panic/DPanic logs. |
| 136 | // Zap will call this hook after writing a log statement with a Panic/DPanic level. |
| 137 | // |
| 138 | // For example, the following builds a logger that will exit the current |
| 139 | // goroutine after writing a Panic/DPanic log message, but it will not start a panic. |
| 140 | // |
| 141 | // zap.New(core, zap.WithPanicHook(zapcore.WriteThenGoexit)) |
| 142 | // |
| 143 | // This is useful for testing Panic/DPanic log output. |
| 144 | func WithPanicHookscip-go gomod go.uber.org/zap . `go.uber.org/zap`/WithPanicHook().(hook zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.CheckWriteHookscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckWriteHook#) Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 145 | return optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#(func(log *Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) { |
| 146 | log.onPanicscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#onPanic. = hook |
| 147 | }) |
| 148 | } |
| 149 | |
| 150 | // OnFatal sets the action to take on fatal logs. |
| 151 | // |
| 152 | // Deprecated: Use [WithFatalHook] instead. |
| 153 | func OnFatalscip-go gomod go.uber.org/zap . `go.uber.org/zap`/OnFatal().(action zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.CheckWriteActionscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckWriteAction#) Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 154 | return WithFatalHookscip-go gomod go.uber.org/zap . `go.uber.org/zap`/WithFatalHook().(action) |
| 155 | } |
| 156 | |
| 157 | // WithFatalHook sets a CheckWriteHook to run on fatal logs. |
| 158 | // Zap will call this hook after writing a log statement with a Fatal level. |
| 159 | // |
| 160 | // For example, the following builds a logger that will exit the current |
| 161 | // goroutine after writing a fatal log message, but it will not exit the |
| 162 | // program. |
| 163 | // |
| 164 | // zap.New(core, zap.WithFatalHook(zapcore.WriteThenGoexit)) |
| 165 | // |
| 166 | // It is important that the provided CheckWriteHook stops the control flow at |
| 167 | // the current statement to meet expectations of callers of the logger. |
| 168 | // We recommend calling os.Exit or runtime.Goexit inside custom hooks at |
| 169 | // minimum. |
| 170 | func WithFatalHookscip-go gomod go.uber.org/zap . `go.uber.org/zap`/WithFatalHook().(hook zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.CheckWriteHookscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/CheckWriteHook#) Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 171 | return optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#(func(log *Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) { |
| 172 | log.onFatalscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#onFatal. = hook |
| 173 | }) |
| 174 | } |
| 175 | |
| 176 | // WithClock specifies the clock used by the logger to determine the current |
| 177 | // time for logged entries. Defaults to the system clock with time.Now. |
| 178 | func WithClockscip-go gomod go.uber.org/zap . `go.uber.org/zap`/WithClock().(clock zapcorescip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/.Clockscip-go gomod go.uber.org/zap . `go.uber.org/zap/zapcore`/Clock#) Optionscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Option# { |
| 179 | return optionFuncscip-go gomod go.uber.org/zap . `go.uber.org/zap`/optionFunc#(func(log *Loggerscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#) { |
| 180 | log.clockscip-go gomod go.uber.org/zap . `go.uber.org/zap`/Logger#clock. = clock |
| 181 | }) |
| 182 | } |