From 410466b3af8af7c24fbd25285b7b76476782d5f2 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Thu, 16 Sep 2021 12:32:28 +0000 Subject: [PATCH] fix: cannot call SetPrimaryCore after using a Tee logger Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- core.go | 5 +++-- setup_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/core.go b/core.go index 87e7d9c..56672d3 100644 --- a/core.go +++ b/core.go @@ -1,6 +1,7 @@ package log import ( + "reflect" "sync" "go.uber.org/multierr" @@ -80,7 +81,7 @@ func (l *lockedMultiCore) DeleteCore(core zapcore.Core) { w := 0 for i := 0; i < len(l.cores); i++ { - if l.cores[i] == core { + if reflect.DeepEqual(l.cores[i], core) { continue } l.cores[w] = l.cores[i] @@ -94,7 +95,7 @@ func (l *lockedMultiCore) ReplaceCore(original, replacement zapcore.Core) { defer l.mu.Unlock() for i := 0; i < len(l.cores); i++ { - if l.cores[i] == original { + if reflect.DeepEqual(l.cores[i], original) { l.cores[i] = replacement } } diff --git a/setup_test.go b/setup_test.go index d3b1c28..57fbd8b 100644 --- a/setup_test.go +++ b/setup_test.go @@ -7,6 +7,9 @@ import ( "os" "strings" "testing" + + "go.uber.org/zap" + "go.uber.org/zap/zapcore" ) func TestGetLoggerDefault(t *testing.T) { @@ -240,3 +243,18 @@ func TestCustomCore(t *testing.T) { t.Errorf("got %q, wanted it to contain log output", buf2.String()) } } + +func TestTeeCore(t *testing.T) { + // configure to use a tee logger + tee := zap.New(zapcore.NewTee( + zap.NewNop().Core(), + zap.NewNop().Core(), + ), zap.AddCaller()) + SetPrimaryCore(tee.Core()) + log := getLogger("test") + log.Error("scooby") + + // replaces the tee logger with a simple one + SetPrimaryCore(zap.NewNop().Core()) + log.Error("doo") +}