@@ -30,6 +30,8 @@ import (
30
30
"go.uber.org/zap/zapcore"
31
31
)
32
32
33
+ var encVal encoderFlag
34
+
33
35
// New returns a brand new Logger configured with Opts. It
34
36
// uses KubeAwareEncoder which adds Type information and
35
37
// Namespace/Name to the log.
@@ -132,6 +134,8 @@ type Options struct {
132
134
// Encoder configures how Zap will encode the output. Defaults to
133
135
// console when Development is true and JSON otherwise
134
136
Encoder zapcore.Encoder
137
+ // TimeEncoder configures the zap time format. Defaults to epoch.
138
+ TimeEncoder zapcore.TimeEncoder
135
139
// DestWritter controls the destination of the log output. Defaults to
136
140
// os.Stderr.
137
141
DestWritter io.Writer
@@ -155,8 +159,10 @@ func (o *Options) addDefaults() {
155
159
156
160
if o .Development {
157
161
if o .Encoder == nil {
158
- encCfg := zap .NewDevelopmentEncoderConfig ()
159
- o .Encoder = zapcore .NewConsoleEncoder (encCfg )
162
+ if o .TimeEncoder != nil {
163
+ ecfs = append (ecfs , withTimeEncoding (o .TimeEncoder ))
164
+ }
165
+ o .Encoder = newConsoleEncoder (ecfs )
160
166
}
161
167
if o .Level == nil {
162
168
lvl := zap .NewAtomicLevelAt (zap .DebugLevel )
@@ -170,8 +176,10 @@ func (o *Options) addDefaults() {
170
176
171
177
} else {
172
178
if o .Encoder == nil {
173
- encCfg := zap .NewProductionEncoderConfig ()
174
- o .Encoder = zapcore .NewJSONEncoder (encCfg )
179
+ if o .TimeEncoder != nil {
180
+ ecfs = append (ecfs , withTimeEncoding (o .TimeEncoder ))
181
+ }
182
+ o .Encoder = newJSONEncoder (ecfs )
175
183
}
176
184
if o .Level == nil {
177
185
lvl := zap .NewAtomicLevelAt (zap .InfoLevel )
@@ -223,8 +231,14 @@ func (o *Options) BindFlags(fs *flag.FlagSet) {
223
231
"Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn). " +
224
232
"Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)" )
225
233
234
+ // Set TimeEncoder value
235
+ var timeEncVal timeEncoderFlag
236
+ timeEncVal .setFunc = func (fromFlag zapcore.TimeEncoder ) {
237
+ o .TimeEncoder = fromFlag
238
+ }
239
+ fs .Var (& timeEncVal , "zap-time-encoder" , "Zap time encoding format('rfc3339', 'rfc339nanos', 'epoch', 'millis', 'nano', or 'iso8601')" )
240
+
226
241
// Set Encoder value
227
- var encVal encoderFlag
228
242
encVal .setFunc = func (fromFlag zapcore.Encoder ) {
229
243
o .Encoder = fromFlag
230
244
}
@@ -255,6 +269,14 @@ func (o *Options) BindFlags(fs *flag.FlagSet) {
255
269
func UseFlagOptions (in * Options ) Opts {
256
270
return func (o * Options ) {
257
271
* o = * in
272
+ if o .Encoder != nil && o .TimeEncoder != nil {
273
+ ecfs = append (ecfs , withTimeEncoding (o .TimeEncoder ))
274
+ if encVal .value == "json" {
275
+ o .Encoder = newJSONEncoder (ecfs )
276
+ } else {
277
+ o .Encoder = newConsoleEncoder (ecfs )
278
+ }
279
+ }
258
280
o .addDefaults ()
259
281
}
260
282
}
0 commit comments