1 // Copyright (c) 2016 Uber Technologies, Inc.
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
23 import "go.uber.org/zap/zapcore"
25 // An Option configures a Logger.
26 type Option interface {
30 // optionFunc wraps a func so it satisfies the Option interface.
31 type optionFunc func(*Logger)
33 func (f optionFunc) apply(log *Logger) {
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)
44 // Hooks registers functions which will be called each time the Logger writes
45 // out an Entry. Repeated use of Hooks is additive.
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...)
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)
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.
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) {
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
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) {
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
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) {