@@ -537,24 +537,78 @@ func TestTypedLoggerCoreSync(t *testing.T) {
537537}
538538
539539func TestTypedLoggerCoreWith (t * testing.T ) {
540- defaultCoreMock := & ZapCoreMock {}
541- typedCoreMock := & ZapCoreMock {}
540+ defaultWriter := writeSyncer {}
541+ typedWriter := writeSyncer {}
542+
543+ cfg := zap .NewProductionEncoderConfig ()
544+ cfg .TimeKey = "" // remove the time to make the log entry consistent
545+
546+ defaultCore := zapcore .NewCore (
547+ zapcore .NewJSONEncoder (cfg ),
548+ & defaultWriter ,
549+ zapcore .InfoLevel ,
550+ )
551+
552+ typedCore := zapcore .NewCore (
553+ zapcore .NewJSONEncoder (cfg ),
554+ & typedWriter ,
555+ zapcore .InfoLevel ,
556+ )
542557
543- defaultCoreMock .WithFunc = func (fields []zapcore.Field ) zapcore.Core { return defaultCoreMock }
544- typedCoreMock .WithFunc = func (fields []zapcore.Field ) zapcore.Core { return typedCoreMock }
545558 core := typedLoggerCore {
546- defaultCore : defaultCoreMock ,
547- typedCore : typedCoreMock ,
559+ defaultCore : defaultCore ,
560+ typedCore : typedCore ,
561+ key : "log.type" ,
562+ value : "sensitive" ,
548563 }
549564
550- field := strField ("foo" , "bar" )
551- core .With ([]zapcore.Field {field })
565+ expectedLines := []string {
566+ // First/Default logger
567+ `{"level":"info","msg":"Very first message"}` ,
568+
569+ // Two messages after calling With
570+ `{"level":"info","msg":"a message with extra fields","foo":"bar"}` ,
571+ `{"level":"info","msg":"another message with extra fields","foo":"bar"}` ,
572+
573+ // A message with the default logger
574+ `{"level":"info","msg":"a message without extra fields"}` ,
575+
576+ // Two more messages with a different field
577+ `{"level":"info","msg":"a message with an answer","answer":"42"}` ,
578+ `{"level":"info","msg":"another message with an answer","answer":"42"}` ,
552579
553- if core . defaultCore != defaultCoreMock {
554- t . Error ( "defaultCore must not change after call to With" )
580+ // One last message with the default logger
581+ `{"level":"info","msg":"another message without any extra fields"}` ,
555582 }
556- if core .typedCore != typedCoreMock {
557- t .Error ("typedCore must not change after call to With" )
583+
584+ // The default logger, it should not be modified by any call to With.
585+ logger := zap .New (& core )
586+ logger .Info ("Very first message" )
587+
588+ // Add a field and write messages
589+ loggerWithFields := logger .With (strField ("foo" , "bar" ))
590+ loggerWithFields .Info ("a message with extra fields" )
591+ loggerWithFields .Info ("another message with extra fields" )
592+
593+ // Use the default logger again
594+ logger .Info ("a message without extra fields" )
595+
596+ // New logger with other fields
597+ loggerWithFields = logger .With (strField ("answer" , "42" ))
598+ loggerWithFields .Info ("a message with an answer" )
599+ loggerWithFields .Info ("another message with an answer" )
600+
601+ // One last message with the default logger
602+ logger .Info ("another message without any extra fields" )
603+
604+ scanner := bufio .NewScanner (strings .NewReader (defaultWriter .String ()))
605+ count := 0
606+ for scanner .Scan () {
607+ l := scanner .Text ()
608+ if l != expectedLines [count ] {
609+ t .Error ("Expecting:\n " , l , "\n Got:\n " , expectedLines [count ])
610+ }
611+ count ++
558612 }
559613}
560614
0 commit comments