Skip to content

Commit a5c9071

Browse files
authored
fix_: remove references to logger's core children (#6465)
References prevented GC to remove unused loggers. fixes: #6402
1 parent 8557425 commit a5c9071

File tree

2 files changed

+12
-44
lines changed

2 files changed

+12
-44
lines changed

logutils/core.go

-21
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ type Core struct {
2222
encoder atomic.Value // encoderWrapper
2323
syncer *atomic.Value // writeSyncerWrapper
2424
level zap.AtomicLevel
25-
26-
next *Core
27-
nextFields []zapcore.Field
2825
}
2926

3027
var (
@@ -70,9 +67,6 @@ func (core *Core) With(fields []zapcore.Field) zapcore.Core {
7067
clone := *core
7168
clone.encoder.Store(clonedEncoder)
7269

73-
core.next = &clone
74-
core.nextFields = fields
75-
7670
return &clone
7771
}
7872

@@ -109,18 +103,3 @@ func (core *Core) Sync() error {
109103
func (core *Core) UpdateSyncer(newSyncer zapcore.WriteSyncer) {
110104
core.syncer.Store(writeSyncerWrapper{WriteSyncer: newSyncer})
111105
}
112-
113-
func (core *Core) UpdateEncoder(newEncoder zapcore.Encoder) {
114-
core.encoder.Store(encoderWrapper{Encoder: newEncoder})
115-
116-
// Update next Cores with newEncoder
117-
current := core
118-
for current.next != nil {
119-
clonedEncoder := encoderWrapper{Encoder: core.getEncoder().Clone()}
120-
for i := range core.nextFields {
121-
current.nextFields[i].AddTo(clonedEncoder)
122-
}
123-
current.next.encoder.Store(clonedEncoder)
124-
current = current.next
125-
}
126-
}

logutils/core_test.go

+12-23
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package logutils
22

33
import (
44
"bytes"
5-
"fmt"
65
"strings"
76
"sync"
87
"testing"
@@ -30,35 +29,25 @@ func TestCore(t *testing.T) {
3029
childWithMoreContext := childWithContext.With(zap.String("key2", "value2"))
3130
grandChild := childWithMoreContext.Named("grandChild")
3231

33-
parent.Debug("Status")
34-
child.Debug("Super")
35-
childWithContext.Debug("App")
36-
childWithMoreContext.Debug("The")
37-
grandChild.Debug("Best")
38-
39-
core.UpdateSyncer(zapcore.AddSync(buffer2))
40-
core.UpdateEncoder(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()))
41-
42-
parent.Debug("Status")
43-
child.Debug("Super")
44-
childWithContext.Debug("App")
45-
childWithMoreContext.Debug("The")
46-
grandChild.Debug("Best")
47-
48-
fmt.Println(buffer1.String())
49-
fmt.Println(buffer2.String())
32+
print := func() {
33+
parent.Debug("Status")
34+
child.Debug("Super")
35+
childWithContext.Debug("App")
36+
childWithMoreContext.Debug("The")
37+
grandChild.Debug("Best")
38+
}
5039

5140
// Ensure that the first buffer has the console encoder output
41+
print()
5242
buffer1Lines := strings.Split(buffer1.String(), "\n")
5343
require.Len(t, buffer1Lines, 5+1)
5444
require.Regexp(t, `\s+child\s+`, buffer1Lines[1])
5545
require.Regexp(t, `\s+child\.grandChild\s+`, buffer1Lines[4])
5646

57-
// Ensure that the second buffer has the JSON encoder output
58-
buffer2Lines := strings.Split(buffer2.String(), "\n")
59-
require.Len(t, buffer2Lines, 5+1)
60-
require.Regexp(t, `"logger"\s*:\s*"child"`, buffer2Lines[1])
61-
require.Regexp(t, `"logger"\s*:\s*"child\.grandChild"`, buffer2Lines[4])
47+
// Ensure syncer was updated
48+
core.UpdateSyncer(zapcore.AddSync(buffer2))
49+
print()
50+
require.Equal(t, buffer1, buffer2)
6251
}
6352

6453
func benchmarkCore(b *testing.B, core zapcore.Core) {

0 commit comments

Comments
 (0)