Skip to content

Commit afc44c7

Browse files
committed
Fix behaviour of typedCore.With
1 parent ec4a1aa commit afc44c7

3 files changed

Lines changed: 74 additions & 16 deletions

File tree

logp/configure/logging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func LoggingWithTypedOutputs(beatName string, cfg, typedCfg *config.C, logKey, k
9696
typedLogpConfig.Beat = beatName
9797
if typedCfg != nil {
9898
if err := typedCfg.Unpack(&typedLogpConfig); err != nil {
99-
return fmt.Errorf("cannot unpack sensitiveCfg: %w", err)
99+
return fmt.Errorf("cannot unpack typed output config: %w", err)
100100
}
101101
}
102102

logp/core_test.go

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -537,24 +537,78 @@ func TestTypedLoggerCoreSync(t *testing.T) {
537537
}
538538

539539
func 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, "\nGot:\n", expectedLines[count])
610+
}
611+
count++
558612
}
559613
}
560614

logp/typedloggercore.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ func (t *typedLoggerCore) Enabled(l zapcore.Level) bool {
4343
}
4444

4545
func (t *typedLoggerCore) With(fields []zapcore.Field) zapcore.Core {
46-
t.defaultCore = t.defaultCore.With(fields)
47-
t.typedCore = t.typedCore.With(fields)
48-
return t
46+
newCore := typedLoggerCore{
47+
defaultCore: t.defaultCore.With(fields),
48+
typedCore: t.typedCore.With(fields),
49+
key: t.key,
50+
value: t.value,
51+
}
52+
return &newCore
4953
}
5054

5155
func (t *typedLoggerCore) Check(e zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {

0 commit comments

Comments
 (0)