@@ -30,6 +30,12 @@ import (
30
30
"go.uber.org/zap/zapcore"
31
31
)
32
32
33
+ // EncoderConfigOption is a function that can modify a `zapcore.EncoderConfig`.
34
+ type EncoderConfigOption func (* zapcore.EncoderConfig )
35
+
36
+ // NewEncoderFunc is a function that creates an Encoder using the provided EncoderConfigOptions.
37
+ type NewEncoderFunc func (... EncoderConfigOption ) zapcore.Encoder
38
+
33
39
// New returns a brand new Logger configured with Opts. It
34
40
// uses KubeAwareEncoder which adds Type information and
35
41
// Namespace/Name to the log.
@@ -65,6 +71,22 @@ func Encoder(encoder zapcore.Encoder) func(o *Options) {
65
71
}
66
72
}
67
73
74
+ func newJSONEncoder (opts ... EncoderConfigOption ) zapcore.Encoder {
75
+ encoderConfig := zap .NewProductionEncoderConfig ()
76
+ for _ , opt := range opts {
77
+ opt (& encoderConfig )
78
+ }
79
+ return zapcore .NewJSONEncoder (encoderConfig )
80
+ }
81
+
82
+ func newConsoleEncoder (opts ... EncoderConfigOption ) zapcore.Encoder {
83
+ encoderConfig := zap .NewDevelopmentEncoderConfig ()
84
+ for _ , opt := range opts {
85
+ opt (& encoderConfig )
86
+ }
87
+ return zapcore .NewConsoleEncoder (encoderConfig )
88
+ }
89
+
68
90
// Level sets the the minimum enabled logging level e.g Debug, Info
69
91
// See Options.Level
70
92
func Level (level zapcore.LevelEnabler ) func (o * Options ) {
@@ -99,6 +121,14 @@ type Options struct {
99
121
// Encoder configures how Zap will encode the output. Defaults to
100
122
// console when Development is true and JSON otherwise
101
123
Encoder zapcore.Encoder
124
+ // EncoderConfigOptions can modify the EncoderConfig needed to initialize an Encoder.
125
+ // See https://godoc.org/go.uber.org/zap/zapcore#EncoderConfig for the list of options
126
+ // that can be configured.
127
+ // Note that the EncoderConfigOptions are not applied when the Encoder option is already set.
128
+ EncoderConfigOptions []EncoderConfigOption
129
+ // NewEncoder configures Encoder using the provided EncoderConfigOptions.
130
+ // Note that the NewEncoder function is not used when the Encoder option is already set.
131
+ NewEncoder NewEncoderFunc
102
132
// DestWritter controls the destination of the log output. Defaults to
103
133
// os.Stderr.
104
134
DestWritter io.Writer
@@ -121,9 +151,8 @@ func (o *Options) addDefaults() {
121
151
}
122
152
123
153
if o .Development {
124
- if o .Encoder == nil {
125
- encCfg := zap .NewDevelopmentEncoderConfig ()
126
- o .Encoder = zapcore .NewConsoleEncoder (encCfg )
154
+ if o .NewEncoder == nil {
155
+ o .NewEncoder = newConsoleEncoder
127
156
}
128
157
if o .Level == nil {
129
158
lvl := zap .NewAtomicLevelAt (zap .DebugLevel )
@@ -136,9 +165,8 @@ func (o *Options) addDefaults() {
136
165
o .ZapOpts = append (o .ZapOpts , zap .Development ())
137
166
138
167
} else {
139
- if o .Encoder == nil {
140
- encCfg := zap .NewProductionEncoderConfig ()
141
- o .Encoder = zapcore .NewJSONEncoder (encCfg )
168
+ if o .NewEncoder == nil {
169
+ o .NewEncoder = newJSONEncoder
142
170
}
143
171
if o .Level == nil {
144
172
lvl := zap .NewAtomicLevelAt (zap .InfoLevel )
@@ -157,6 +185,9 @@ func (o *Options) addDefaults() {
157
185
}))
158
186
}
159
187
}
188
+ if o .Encoder == nil {
189
+ o .Encoder = o .NewEncoder (o .EncoderConfigOptions ... )
190
+ }
160
191
o .ZapOpts = append (o .ZapOpts , zap .AddStacktrace (o .StacktraceLevel ))
161
192
}
162
193
@@ -182,7 +213,7 @@ func NewRaw(opts ...Opts) *zap.Logger {
182
213
// BindFlags will parse the given flagset for zap option flags and set the log options accordingly
183
214
// zap-devel: Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn)
184
215
// Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)
185
- // zap-encoder: Zap log encoding ('json' or 'console')
216
+ // zap-encoder: Zap log encoding (one of 'json' or 'console')
186
217
// zap-log-level: Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error',
187
218
// or any integer value > 0 which corresponds to custom debug levels of increasing verbosity")
188
219
// zap-stacktrace-level: Zap Level at and above which stacktraces are captured (one of 'info' or 'error')
@@ -195,10 +226,10 @@ func (o *Options) BindFlags(fs *flag.FlagSet) {
195
226
196
227
// Set Encoder value
197
228
var encVal encoderFlag
198
- encVal .setFunc = func (fromFlag zapcore. Encoder ) {
199
- o .Encoder = fromFlag
229
+ encVal .setFunc = func (fromFlag NewEncoderFunc ) {
230
+ o .NewEncoder = fromFlag
200
231
}
201
- fs .Var (& encVal , "zap-encoder" , "Zap log encoding ('json' or 'console')" )
232
+ fs .Var (& encVal , "zap-encoder" , "Zap log encoding (one of 'json' or 'console')" )
202
233
203
234
// Set the Log Level
204
235
var levelVal levelFlag
@@ -221,10 +252,10 @@ func (o *Options) BindFlags(fs *flag.FlagSet) {
221
252
// UseFlagOptions configures the logger to use the Options set by parsing zap option flags from the CLI.
222
253
// opts := zap.Options{}
223
254
// opts.BindFlags(flag.CommandLine)
255
+ // flag.Parse()
224
256
// log := zap.New(zap.UseFlagOptions(&opts))
225
257
func UseFlagOptions (in * Options ) Opts {
226
258
return func (o * Options ) {
227
259
* o = * in
228
- o .addDefaults ()
229
260
}
230
261
}
0 commit comments