Skip to content

Commit

Permalink
Merge pull request #121 from moul/dev/moul/replace-tee
Browse files Browse the repository at this point in the history
fix: cannot call SetPrimaryCore after using a Tee logger
  • Loading branch information
Stebalien committed Sep 28, 2021
2 parents 73fb407 + 410466b commit b7a50af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions 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 @@ -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]
Expand All @@ -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 b7a50af

Please sign in to comment.