Skip to content

Commit

Permalink
fix: cannot call SetPrimaryCore after using a Tee logger
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
moul committed Sep 16, 2021
1 parent 73fb407 commit 9fc42bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package log

import (
"reflect"
"sync"

"go.uber.org/multierr"
Expand Down Expand Up @@ -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
}
}
Expand Down
18 changes: 18 additions & 0 deletions setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"os"
"strings"
"testing"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func TestGetLoggerDefault(t *testing.T) {
Expand Down Expand Up @@ -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")
}

0 comments on commit 9fc42bc

Please sign in to comment.