From 3600f4aa84da9b85265ccbe6628e9b966c77b6d0 Mon Sep 17 00:00:00 2001 From: Evan Phoenix Date: Tue, 12 Dec 2023 16:39:02 -0800 Subject: [PATCH] Conside if the level is to be used separately from if the levels should 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 --- intlogger.go | 10 ++++--- logger_test.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/intlogger.go b/intlogger.go index 1a80008..104d82f 100644 --- a/intlogger.go +++ b/intlogger.go @@ -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 @@ -892,6 +895,7 @@ 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 { @@ -899,11 +903,11 @@ func (l *intLogger) searchLevelPtr() *int32 { 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 } diff --git a/logger_test.go b/logger_test.go index 1ab9fd5..dea7013 100644 --- a/logger_test.go +++ b/logger_test.go @@ -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{ @@ -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) {