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