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 (#137)

* Conside if the level is to be used separately from if the levels should be calculated

* Add a test for post-SetLevel .Named()


---------

Co-authored-by: Mike Palmiotto <[email protected]>
  • Loading branch information
evanphx and mpalmi authored Dec 13, 2023
1 parent 71d286f commit 3600f4a
Show file tree
Hide file tree
Showing 2 changed files with 79 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
73 changes: 72 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 Expand Up @@ -700,6 +737,40 @@ func TestLogger(t *testing.T) {
s.Equal(Trace, b.GetLevel())
s.Equal(Trace, c.GetLevel())
})
t.Run("level sync example 3", 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")

a.SetLevel(Trace)
s.Equal(Trace, a.GetLevel())
s.Equal(Trace, b.GetLevel())

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

c := a.Named("c")

c.SetLevel(Error)
s.Equal(Trace, 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())
})
}

func TestLogger_leveledWriter(t *testing.T) {
Expand Down

0 comments on commit 3600f4a

Please sign in to comment.