Skip to content

Commit

Permalink
Conside if the level is to be used separately from if the levels shou…
Browse files Browse the repository at this point in the history
…ld be calculated
  • Loading branch information
evanphx committed Dec 12, 2023
1 parent 71d286f commit b295f67
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
10 changes: 7 additions & 3 deletions intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ type intLogger struct {
writer *writer
level *int32

// The value of curEpoch when our level was set
setEpoch uint64

// The value of curEpoch the last time we performed the level sync process
ownEpoch uint64

Expand Down Expand Up @@ -892,18 +895,19 @@ func (l *intLogger) SetLevel(level Level) {
l.level = nsl

l.ownEpoch = atomic.AddUint64(l.curEpoch, 1)
l.setEpoch = l.ownEpoch
}

func (l *intLogger) searchLevelPtr() *int32 {
p := l.parent

ptr := l.level

max := l.ownEpoch
max := l.setEpoch

for p != nil {
if p.ownEpoch > max {
max = p.ownEpoch
if p.setEpoch > max {
max = p.setEpoch
ptr = p.level
}

Expand Down
39 changes: 38 additions & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,44 @@ func TestLogger(t *testing.T) {
assert.Equal(t, Error, b.GetLevel())
})

t.Run("level sync example", func(t *testing.T) {
t.Run("level sync example 1", func(t *testing.T) {
var buf bytes.Buffer

logger := New(&LoggerOptions{
Name: "root",
Output: &buf,
SyncParentLevel: true,
})

s := assert.New(t)

a := logger.Named("a")
b := a.Named("b")
c := a.Named("c")

b.SetLevel(Warn)
s.Equal(Info, a.GetLevel())
s.Equal(Warn, b.GetLevel())
s.Equal(Info, c.GetLevel())

c.SetLevel(Error)
s.Equal(Info, a.GetLevel())
s.Equal(Warn, b.GetLevel())
s.Equal(Error, c.GetLevel())

a.SetLevel(Warn)
s.Equal(Warn, a.GetLevel())
s.Equal(Warn, b.GetLevel())
s.Equal(Warn, c.GetLevel())

logger.SetLevel(Trace)
s.Equal(Trace, logger.GetLevel())
s.Equal(Trace, a.GetLevel())
s.Equal(Trace, b.GetLevel())
s.Equal(Trace, c.GetLevel())
})

t.Run("level sync example 2", func(t *testing.T) {
var buf bytes.Buffer

logger := New(&LoggerOptions{
Expand Down

0 comments on commit b295f67

Please sign in to comment.