@@ -30,6 +30,13 @@ import (
30
30
"go.uber.org/zap/zapcore"
31
31
)
32
32
33
+ // EncoderConfigOption is being used to pass options
34
+ // for configuring console/json encoder.
35
+ type EncoderConfigOption func (* zapcore.EncoderConfig )
36
+
37
+ // NewEncoderFunc is a function that creates an Encoder using the provided EncoderConfigOptions.
38
+ type NewEncoderFunc func (... EncoderConfigOption ) zapcore.Encoder
39
+
33
40
// New returns a brand new Logger configured with Opts. It
34
41
// uses KubeAwareEncoder which adds Type information and
35
42
// Namespace/Name to the log.
@@ -98,6 +105,33 @@ func Encoder(encoder zapcore.Encoder) func(o *Options) {
98
105
}
99
106
}
100
107
108
+ func newJSONEncoder (ecfs ... EncoderConfigOption ) zapcore.Encoder {
109
+ encoderConfig := zap .NewProductionEncoderConfig ()
110
+ for _ , f := range ecfs {
111
+ f (& encoderConfig )
112
+ }
113
+ return zapcore .NewJSONEncoder (encoderConfig )
114
+ }
115
+
116
+ func newConsoleEncoder (ecfs ... EncoderConfigOption ) zapcore.Encoder {
117
+ encoderConfig := zap .NewDevelopmentEncoderConfig ()
118
+ for _ , f := range ecfs {
119
+ f (& encoderConfig )
120
+ }
121
+ return zapcore .NewConsoleEncoder (encoderConfig )
122
+ }
123
+
124
+ // TimeEncoder configures how the logger will encode time format.
125
+ // See Options.EncoderConfigOptions
126
+ func TimeEncoder (timeEncoder zapcore.TimeEncoder ) func (o * Options ) {
127
+ return func (o * Options ) {
128
+ f := func (ec * zapcore.EncoderConfig ) {
129
+ ec .EncodeTime = timeEncoder
130
+ }
131
+ o .EncoderConfigOptions = append (o .EncoderConfigOptions , f )
132
+ }
133
+ }
134
+
101
135
// Level sets the the minimum enabled logging level e.g Debug, Info
102
136
// See Options.Level
103
137
func Level (level zapcore.LevelEnabler ) func (o * Options ) {
@@ -132,6 +166,14 @@ type Options struct {
132
166
// Encoder configures how Zap will encode the output. Defaults to
133
167
// console when Development is true and JSON otherwise
134
168
Encoder zapcore.Encoder
169
+ // EncoderConfigOptions can modify the EncoderConfig needed to initialize an Encoder.
170
+ // See https://godoc.org/go.uber.org/zap/zapcore#EncoderConfig for the list of options
171
+ // that can be configured.
172
+ // Note that the EncoderConfigOptions are not applied when the Encoder option is already set.
173
+ EncoderConfigOptions []EncoderConfigOption
174
+ // NewEncoder configures Encoder using the provided EncoderConfigOptions.
175
+ // Note that the NewEncoder function is not used when the Encoder option is already set.
176
+ NewEncoder NewEncoderFunc
135
177
// DestWritter controls the destination of the log output. Defaults to
136
178
// os.Stderr.
137
179
DestWritter io.Writer
@@ -154,9 +196,8 @@ func (o *Options) addDefaults() {
154
196
}
155
197
156
198
if o .Development {
157
- if o .Encoder == nil {
158
- encCfg := zap .NewDevelopmentEncoderConfig ()
159
- o .Encoder = zapcore .NewConsoleEncoder (encCfg )
199
+ if o .NewEncoder == nil {
200
+ o .NewEncoder = newConsoleEncoder
160
201
}
161
202
if o .Level == nil {
162
203
lvl := zap .NewAtomicLevelAt (zap .DebugLevel )
@@ -169,9 +210,8 @@ func (o *Options) addDefaults() {
169
210
o .ZapOpts = append (o .ZapOpts , zap .Development ())
170
211
171
212
} else {
172
- if o .Encoder == nil {
173
- encCfg := zap .NewProductionEncoderConfig ()
174
- o .Encoder = zapcore .NewJSONEncoder (encCfg )
213
+ if o .NewEncoder == nil {
214
+ o .NewEncoder = newJSONEncoder
175
215
}
176
216
if o .Level == nil {
177
217
lvl := zap .NewAtomicLevelAt (zap .InfoLevel )
@@ -186,7 +226,9 @@ func (o *Options) addDefaults() {
186
226
return zapcore .NewSampler (core , time .Second , 100 , 100 )
187
227
}))
188
228
}
189
-
229
+ if o .Encoder == nil {
230
+ o .Encoder = o .NewEncoder (o .EncoderConfigOptions ... )
231
+ }
190
232
o .ZapOpts = append (o .ZapOpts , zap .AddStacktrace (o .StacktraceLevel ))
191
233
}
192
234
@@ -212,7 +254,8 @@ func NewRaw(opts ...Opts) *zap.Logger {
212
254
// BindFlags will parse the given flagset for zap option flags and set the log options accordingly
213
255
// zap-devel: Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn)
214
256
// Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)
215
- // zap-encoder: Zap log encoding ('json' or 'console')
257
+ // zap-encoder: Zap log encoding (Eg., 'json' or 'console')
258
+ // zap-time-encoder: Zap time encoding format, defaults to 'epoch'.
216
259
// zap-log-level: Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error',
217
260
// or any integer value > 0 which corresponds to custom debug levels of increasing verbosity")
218
261
// zap-stacktrace-level: Zap Level at and above which stacktraces are captured (one of 'warn' or 'error')
@@ -223,12 +266,20 @@ func (o *Options) BindFlags(fs *flag.FlagSet) {
223
266
"Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn). " +
224
267
"Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)" )
225
268
269
+ // Set TimeEncoder value
270
+ var timeEncVal timeEncoderFlag
271
+ timeEncVal .setFunc = func (fromFlag zapcore.TimeEncoder ) {
272
+ // Set o.EncoderConfigOptions with zap-time-encoder
273
+ TimeEncoder (fromFlag )(o )
274
+ }
275
+ fs .Var (& timeEncVal , "zap-time-encoder" , "Zap time encoding format, defaults to 'epoch'" )
276
+
226
277
// Set Encoder value
227
278
var encVal encoderFlag
228
- encVal .setFunc = func (fromFlag zapcore. Encoder ) {
229
- o .Encoder = fromFlag
279
+ encVal .setFunc = func (fromFlag NewEncoderFunc ) {
280
+ o .NewEncoder = fromFlag
230
281
}
231
- fs .Var (& encVal , "zap-encoder" , "Zap log encoding ('json' or 'console')" )
282
+ fs .Var (& encVal , "zap-encoder" , "Zap log encoding (Eg., 'json' or 'console')" )
232
283
233
284
// Set the Log Level
234
285
var levelVal levelFlag
@@ -251,10 +302,10 @@ func (o *Options) BindFlags(fs *flag.FlagSet) {
251
302
// UseFlagOptions configures the logger to use the Options set by parsing zap option flags from the CLI.
252
303
// opts := zap.Options{}
253
304
// opts.BindFlags(flag.CommandLine)
305
+ // flag.Parse()
254
306
// log := zap.New(zap.UseFlagOptions(&opts))
255
307
func UseFlagOptions (in * Options ) Opts {
256
308
return func (o * Options ) {
257
309
* o = * in
258
- o .addDefaults ()
259
310
}
260
311
}
0 commit comments