@@ -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.
@@ -98,6 +100,14 @@ func Encoder(encoder zapcore.Encoder) func(o *Options) {
98
100
}
99
101
}
100
102
103
+ // TimeEncoder configures how the logger will encode time format.
104
+ // See Options.TimeEncoder
105
+ func TimeEncoder (timeEncoder zapcore.TimeEncoder ) func (o * Options ) {
106
+ return func (o * Options ) {
107
+ o .TimeEncoder = timeEncoder
108
+ }
109
+ }
110
+
101
111
// Level sets the the minimum enabled logging level e.g Debug, Info
102
112
// See Options.Level
103
113
func Level (level * zap.AtomicLevel ) func (o * Options ) {
@@ -132,6 +142,8 @@ type Options struct {
132
142
// Encoder configures how Zap will encode the output. Defaults to
133
143
// console when Development is true and JSON otherwise
134
144
Encoder zapcore.Encoder
145
+ // TimeEncoder configures the zap time format. Defaults to epoch.
146
+ TimeEncoder zapcore.TimeEncoder
135
147
// DestWritter controls the destination of the log output. Defaults to
136
148
// os.Stderr.
137
149
DestWritter io.Writer
@@ -155,8 +167,10 @@ func (o *Options) addDefaults() {
155
167
156
168
if o .Development {
157
169
if o .Encoder == nil {
158
- encCfg := zap .NewDevelopmentEncoderConfig ()
159
- o .Encoder = zapcore .NewConsoleEncoder (encCfg )
170
+ if o .TimeEncoder != nil {
171
+ ecfs = append (ecfs , withTimeEncoding (o .TimeEncoder ))
172
+ }
173
+ o .Encoder = newConsoleEncoder (ecfs )
160
174
}
161
175
if o .Level == nil {
162
176
lvl := zap .NewAtomicLevelAt (zap .DebugLevel )
@@ -170,8 +184,10 @@ func (o *Options) addDefaults() {
170
184
171
185
} else {
172
186
if o .Encoder == nil {
173
- encCfg := zap .NewProductionEncoderConfig ()
174
- o .Encoder = zapcore .NewJSONEncoder (encCfg )
187
+ if o .TimeEncoder != nil {
188
+ ecfs = append (ecfs , withTimeEncoding (o .TimeEncoder ))
189
+ }
190
+ o .Encoder = newJSONEncoder (ecfs )
175
191
}
176
192
if o .Level == nil {
177
193
lvl := zap .NewAtomicLevelAt (zap .InfoLevel )
@@ -223,8 +239,14 @@ func (o *Options) BindFlags(fs *flag.FlagSet) {
223
239
"Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn). " +
224
240
"Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)" )
225
241
242
+ // Set TimeEncoder value
243
+ var timeEncVal timeEncoderFlag
244
+ timeEncVal .setFunc = func (fromFlag zapcore.TimeEncoder ) {
245
+ o .TimeEncoder = fromFlag
246
+ }
247
+ fs .Var (& timeEncVal , "zap-time-encoder" , "Zap time encoding format('rfc3339', 'rfc339nanos', 'epoch', 'millis', 'nano', or 'iso8601')" )
248
+
226
249
// Set Encoder value
227
- var encVal encoderFlag
228
250
encVal .setFunc = func (fromFlag zapcore.Encoder ) {
229
251
o .Encoder = fromFlag
230
252
}
@@ -255,6 +277,14 @@ func (o *Options) BindFlags(fs *flag.FlagSet) {
255
277
func UseFlagOptions (in * Options ) Opts {
256
278
return func (o * Options ) {
257
279
* o = * in
280
+ if o .Encoder != nil && o .TimeEncoder != nil {
281
+ ecfs = append (ecfs , withTimeEncoding (o .TimeEncoder ))
282
+ if encVal .value == "json" {
283
+ o .Encoder = newJSONEncoder (ecfs )
284
+ } else {
285
+ o .Encoder = newConsoleEncoder (ecfs )
286
+ }
287
+ }
258
288
o .addDefaults ()
259
289
}
260
290
}
0 commit comments