@@ -341,91 +341,50 @@ var _ = Describe("Zap flag options setup", func() {
341
341
342
342
})
343
343
})
344
- Context ("with direct Options provided, and no Flag values" , func () {
345
344
346
- It ("Should set dev=true, and default ConsoleEncoder with EpochTime stated in o.addDefaults()" , func () {
345
+ Context ("with encoder options provided." , func () {
346
+
347
+ It ("Should set Console Encoder, with given Nanos TimEncoder option." , func () {
347
348
logOut := new (bytes.Buffer )
348
- log := New (UseDevMode (true ), WriteTo (logOut ))
349
+ f := func (ec * zapcore.EncoderConfig ) {
350
+ if err := ec .EncodeTime .UnmarshalText ([]byte ("nanos" )); err != nil {
351
+ Expect (err ).ToNot (HaveOccurred ())
352
+ }
353
+ }
354
+ opts := func (o * Options ) {
355
+ o .EncoderConfigOptions = append (o .EncoderConfigOptions , f )
356
+ }
357
+ log := New (UseDevMode (true ), WriteTo (logOut ), opts )
349
358
log .Info ("This is a test message" )
350
359
outRaw := logOut .Bytes ()
351
- // Assert for default Epoch Time format
352
- Expect (string (outRaw )).Should (ContainSubstring ("-" ))
353
360
// Assert for Console Encoder
354
361
res := map [string ]interface {}{}
355
362
Expect (json .Unmarshal (outRaw , & res )).ToNot (Succeed ())
356
-
357
- })
358
- It ("Should set dev=false, and JSON encoder with EpochNanosTime stated in o.addDefaults()" , func () {
359
- logOut := new (bytes.Buffer )
360
- log := New (UseDevMode (false ), TimeEncoder (zapcore .EpochNanosTimeEncoder ), WriteTo (logOut ))
361
- log .Info ("This is a test message" )
362
- outRaw := logOut .Bytes ()
363
- // Assert for Epoch Nanos time format
363
+ // Assert for Epoch Nanos TimeEncoder
364
364
Expect (string (outRaw )).ShouldNot (ContainSubstring ("." ))
365
- // Assert for JSON Encoder
366
- res := map [string ]interface {}{}
367
- Expect (json .Unmarshal (outRaw , & res )).To (Succeed ())
368
365
369
366
})
370
- })
371
-
372
- Context ("with -zap-encoder and -zap-time-encoder options provided." , func () {
373
-
374
- It ("Should set JSON Encoder, with given TimEncoder options." , func () {
375
- args := []string {"-zap-encoder=json" , "-zap-time-encoder=millis" }
376
- fromFlags .BindFlags (& fs )
377
- if err := fs .Parse (args ); err != nil {
378
- Expect (err ).ToNot (HaveOccurred ())
379
- }
380
-
367
+ It ("Should set JSON Encoder, with given Millis TimEncoder option, and MessageKey" , func () {
381
368
logOut := new (bytes.Buffer )
382
- log := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
383
- log .Info ("This is a test message" )
384
- outRaw := logOut .Bytes ()
385
-
386
- // Assert for millis Time format
387
- Expect (string (outRaw )).Should (ContainSubstring ("." ))
388
- // Assert for JSON Encoder
389
- res := map [string ]interface {}{}
390
- Expect (json .Unmarshal (outRaw , & res )).To (Succeed ())
391
- })
392
-
393
- It ("Should set Console Encoder, with given TimEncoder options." , func () {
394
- args := []string {"-zap-encoder=console" , "-zap-time-encoder=nanos" }
395
- fromFlags .BindFlags (& fs )
396
- if err := fs .Parse (args ); err != nil {
397
- Expect (err ).ToNot (HaveOccurred ())
369
+ f := func (ec * zapcore.EncoderConfig ) {
370
+ ec .MessageKey = "MillisTimeFormat"
371
+ if err := ec .EncodeTime .UnmarshalText ([]byte ("millis" )); err != nil {
372
+ Expect (err ).ToNot (HaveOccurred ())
373
+ }
398
374
}
399
-
400
- logOut := new (bytes.Buffer )
401
- log := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
402
- log .Info ("This is a test message" )
403
- outRaw := logOut .Bytes ()
404
-
405
- // Assert for nanos Time format
406
- Expect (string (outRaw )).ToNot (ContainSubstring ("." ))
407
- // Assert for CONSOLE Encoder
408
- res := map [string ]interface {}{}
409
- Expect (json .Unmarshal (outRaw , & res )).ToNot (Succeed ())
410
- })
411
-
412
- It ("Should set Console Encoder, with default epoch time format." , func () {
413
- args := []string {"-zap-encoder=console" , "-zap-time-encoder=dummyyyy" }
414
- fromFlags .BindFlags (& fs )
415
- if err := fs .Parse (args ); err != nil {
416
- Expect (err ).ToNot (HaveOccurred ())
375
+ opts := func (o * Options ) {
376
+ o .EncoderConfigOptions = append (o .EncoderConfigOptions , f )
417
377
}
418
-
419
- logOut := new (bytes.Buffer )
420
- log := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
378
+ log := New (UseDevMode (false ), WriteTo (logOut ), opts )
421
379
log .Info ("This is a test message" )
422
380
outRaw := logOut .Bytes ()
423
-
424
- // Assert for default Epoch Time format
425
- Expect (string (outRaw )).To (ContainSubstring ("." ))
426
- // Assert for CONSOLE Encoder
381
+ // Assert for JSON Encoder
427
382
res := map [string ]interface {}{}
428
- Expect (json .Unmarshal (outRaw , & res )).ToNot (Succeed ())
383
+ Expect (json .Unmarshal (outRaw , & res )).To (Succeed ())
384
+ // Assert for Epoch Nanos TimeEncoder
385
+ Expect (string (outRaw )).Should (ContainSubstring ("." ))
386
+ // Assert for MessageKey
387
+ Expect (string (outRaw )).Should (ContainSubstring ("MillisTimeFormat" ))
429
388
})
430
389
431
390
})
0 commit comments