@@ -21,6 +21,7 @@ import (
21
21
"encoding/json"
22
22
"flag"
23
23
"os"
24
+ "reflect"
24
25
25
26
"github.com/go-logr/logr"
26
27
. "github.com/onsi/ginkgo"
@@ -472,35 +473,76 @@ var _ = Describe("Zap log level flag options setup", func() {
472
473
})
473
474
})
474
475
475
- Context ("with encoder options provided programmatically" , func () {
476
+ Context ("with zap-time-encoding flag provided" , func () {
477
+
478
+ It ("Should set time encoder in options" , func () {
479
+ args := []string {"--zap-time-encoding=rfc3339" }
480
+ fromFlags .BindFlags (& fs )
481
+ err := fs .Parse (args )
482
+ Expect (err ).ToNot (HaveOccurred ())
483
+
484
+ opt := Options {}
485
+ UseFlagOptions (& fromFlags )(& opt )
486
+ opt .addDefaults ()
487
+
488
+ optVal := reflect .ValueOf (opt .TimeEncoder )
489
+ expVal := reflect .ValueOf (zapcore .RFC3339TimeEncoder )
490
+
491
+ Expect (optVal .Pointer ()).To (Equal (expVal .Pointer ()))
492
+ })
493
+
494
+ It ("Should default to 'epoch' time encoding" , func () {
495
+ args := []string {"" }
496
+ fromFlags .BindFlags (& fs )
497
+ err := fs .Parse (args )
498
+ Expect (err ).ToNot (HaveOccurred ())
499
+
500
+ opt := Options {}
501
+ UseFlagOptions (& fromFlags )(& opt )
502
+ opt .addDefaults ()
503
+
504
+ optVal := reflect .ValueOf (opt .TimeEncoder )
505
+ expVal := reflect .ValueOf (zapcore .EpochTimeEncoder )
506
+
507
+ Expect (optVal .Pointer ()).To (Equal (expVal .Pointer ()))
508
+ })
509
+
510
+ It ("Should return an error message, with unknown time-encoding" , func () {
511
+ fs = * flag .NewFlagSet (os .Args [0 ], flag .ContinueOnError )
512
+ args := []string {"--zap-time-encoding=foobar" }
513
+ fromFlags .BindFlags (& fs )
514
+ err := fs .Parse (args )
515
+ Expect (err ).To (HaveOccurred ())
516
+ })
517
+
518
+ It ("Should propagate time encoder to logger" , func () {
519
+ // zaps ISO8601TimeEncoder uses 2006-01-02T15:04:05.000Z0700 as pattern for iso8601 encoding
520
+ iso8601Pattern := `^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}(\+[0-9]{4}|Z)`
476
521
477
- It ("Should set Console Encoder, with given Nanos TimeEncoder option." , func () {
522
+ args := []string {"--zap-time-encoding=iso8601" }
523
+ fromFlags .BindFlags (& fs )
524
+ err := fs .Parse (args )
525
+ Expect (err ).ToNot (HaveOccurred ())
478
526
logOut := new (bytes.Buffer )
479
- f := func (ec * zapcore.EncoderConfig ) {
480
- if err := ec .EncodeTime .UnmarshalText ([]byte ("nanos" )); err != nil {
481
- Expect (err ).ToNot (HaveOccurred ())
482
- }
483
- }
484
- opts := func (o * Options ) {
485
- o .EncoderConfigOptions = append (o .EncoderConfigOptions , f )
486
- }
487
- log := New (UseDevMode (true ), WriteTo (logOut ), opts )
488
- log .Info ("This is a test message" )
527
+
528
+ logger := New (UseFlagOptions (& fromFlags ), WriteTo (logOut ))
529
+ logger .Info ("This is a test message" )
530
+
489
531
outRaw := logOut .Bytes ()
490
- // Assert for Console Encoder
491
- res := map [string ]interface {}{}
492
- Expect (json .Unmarshal (outRaw , & res )).ToNot (Succeed ())
493
- // Assert for Epoch Nanos TimeEncoder
494
- Expect (string (outRaw )).ShouldNot (ContainSubstring ("." ))
495
532
533
+ res := map [string ]interface {}{}
534
+ Expect (json .Unmarshal (outRaw , & res )).To (Succeed ())
535
+ Expect (res ["ts" ]).Should (MatchRegexp (iso8601Pattern ))
496
536
})
537
+
538
+ })
539
+
540
+ Context ("with encoder options provided programmatically" , func () {
541
+
497
542
It ("Should set JSON Encoder, with given Millis TimeEncoder option, and MessageKey" , func () {
498
543
logOut := new (bytes.Buffer )
499
544
f := func (ec * zapcore.EncoderConfig ) {
500
545
ec .MessageKey = "MillisTimeFormat"
501
- if err := ec .EncodeTime .UnmarshalText ([]byte ("millis" )); err != nil {
502
- Expect (err ).ToNot (HaveOccurred ())
503
- }
504
546
}
505
547
opts := func (o * Options ) {
506
548
o .EncoderConfigOptions = append (o .EncoderConfigOptions , f )
@@ -511,8 +553,6 @@ var _ = Describe("Zap log level flag options setup", func() {
511
553
// Assert for JSON Encoder
512
554
res := map [string ]interface {}{}
513
555
Expect (json .Unmarshal (outRaw , & res )).To (Succeed ())
514
- // Assert for Epoch Nanos TimeEncoder
515
- Expect (string (outRaw )).Should (ContainSubstring ("." ))
516
556
// Assert for MessageKey
517
557
Expect (string (outRaw )).Should (ContainSubstring ("MillisTimeFormat" ))
518
558
})
0 commit comments