From 3415cf72fb30b5343c4e4dcd093520a34b3f80c7 Mon Sep 17 00:00:00 2001 From: Luiz Aoqui Date: Tue, 15 Oct 2024 11:30:50 -0400 Subject: [PATCH] zerolog: fix log level zerolog.Logger.Level() returns a new logger, which was being ignored, causing SetLevel() to be a no-op. Store the current logger in a pointer that is updated with the new logger whenever SetLevel() is called. Signed-off-by: Luiz Aoqui --- loggers/zerolog/context.go | 6 +---- loggers/zerolog/zerolog.go | 49 ++++++++++++++++++++++++++++++-------- logging_test.go | 18 ++++++++++++++ 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/loggers/zerolog/context.go b/loggers/zerolog/context.go index b97052f..b041413 100644 --- a/loggers/zerolog/context.go +++ b/loggers/zerolog/context.go @@ -17,11 +17,7 @@ type Context struct { } func (c *Context) Logger() types.Logger { - return &Logger{ - logger: c.zeroCtx.Logger(), - source: c.l.source, - level: c.l.level, - } + return newLogger(c.l.source, c.l.level, c.zeroCtx.Logger()) } func (c *Context) Str(key string, val string) types.Context { diff --git a/loggers/zerolog/zerolog.go b/loggers/zerolog/zerolog.go index 28b0361..3d88124 100644 --- a/loggers/zerolog/zerolog.go +++ b/loggers/zerolog/zerolog.go @@ -5,6 +5,7 @@ package zerolog import ( "fmt" "io" + "sync" "github.com/rs/zerolog" @@ -14,9 +15,10 @@ import ( var _ types.RootLogger = (*Logger)(nil) type Logger struct { - level types.Level - logger zerolog.Logger - source string + level types.Level + logger *zerolog.Logger + loggerLock sync.RWMutex + source string } func init() { @@ -25,9 +27,12 @@ func init() { } func New(source string, level types.Level, output io.Writer) *Logger { - zl := zerolog.New(output) + return newLogger(source, level, zerolog.New(output)) +} + +func newLogger(source string, level types.Level, zl zerolog.Logger) *Logger { z := &Logger{ - logger: zl, + logger: &zl, source: source, } z.SetLevel(level) @@ -54,18 +59,24 @@ func (z *Logger) SetLevel(level types.Level) { case types.TraceLevel: zerologLevel = zerolog.TraceLevel } + + newLogger := z.logger.Level(zerologLevel) + + z.loggerLock.Lock() + defer z.loggerLock.Unlock() + + z.logger = &newLogger z.level = level - z.logger.Level(zerologLevel) } func (z *Logger) SubLogger(source string) types.Logger { - return &Logger{ - logger: z.logger, - source: fmt.Sprintf("%s:%s", z.source, source), - } + return newLogger(fmt.Sprintf("%s:%s", z.source, source), z.level, *z.logger) } func (z *Logger) With() types.Context { + z.loggerLock.RLock() + defer z.loggerLock.RUnlock() + return &Context{ l: z, zeroCtx: z.logger.With(), @@ -73,25 +84,43 @@ func (z *Logger) With() types.Context { } func (z *Logger) Fatal() types.Event { + z.loggerLock.RLock() + defer z.loggerLock.RUnlock() + return (*Event)(z.logger.Fatal().Timestamp().Str(types.SourceKey, z.source)) } func (z *Logger) Error() types.Event { + z.loggerLock.RLock() + defer z.loggerLock.RUnlock() + return (*Event)(z.logger.Error().Timestamp().Str(types.SourceKey, z.source)) } func (z *Logger) Warn() types.Event { + z.loggerLock.RLock() + defer z.loggerLock.RUnlock() + return (*Event)(z.logger.Warn().Timestamp().Str(types.SourceKey, z.source)) } func (z *Logger) Info() types.Event { + z.loggerLock.RLock() + defer z.loggerLock.RUnlock() + return (*Event)(z.logger.Info().Timestamp().Str(types.SourceKey, z.source)) } func (z *Logger) Debug() types.Event { + z.loggerLock.RLock() + defer z.loggerLock.RUnlock() + return (*Event)(z.logger.Debug().Timestamp().Str(types.SourceKey, z.source)) } func (z *Logger) Trace() types.Event { + z.loggerLock.RLock() + defer z.loggerLock.RUnlock() + return (*Event)(z.logger.Trace().Timestamp().Str(types.SourceKey, z.source)) } diff --git a/logging_test.go b/logging_test.go index 970c5cb..cbad27f 100644 --- a/logging_test.go +++ b/logging_test.go @@ -66,6 +66,7 @@ func TestInfo(t *testing.T) { out := &bytes.Buffer{} log := New(Noop, t.Name(), out) log.Info().Msg("") + log.Debug().Msg("") // Should not be logged. assert.Equal(t, "", out.String()) }) @@ -73,6 +74,7 @@ func TestInfo(t *testing.T) { out := &bytes.Buffer{} log := New(Zerolog, t.Name(), out) log.Info().Msg("") + log.Debug().Msg("") // Should not be logged. assert.Equal(t, fillZerologTestFields(t, "{\"level\":\"info\",\"time\":\"%s\",\"source\":\"%s\"}\n"), out.String()) }) @@ -80,6 +82,7 @@ func TestInfo(t *testing.T) { out := &bytes.Buffer{} log := New(Slog, t.Name(), out) log.Info().Msg("") + log.Debug().Msg("") // Should not be logged. assert.Equal(t, fillSlogTestFields(t, "level=INFO msg=\"\" source=%s\n"), out.String()) }) }) @@ -89,6 +92,7 @@ func TestInfo(t *testing.T) { out := &bytes.Buffer{} log := New(Noop, t.Name(), out) log.Info().Str("foo", "bar").Msg("") + log.Debug().Str("foo", "bar").Msg("") // Should not be logged. assert.Equal(t, "", out.String()) }) @@ -96,6 +100,7 @@ func TestInfo(t *testing.T) { out := &bytes.Buffer{} log := New(Zerolog, t.Name(), out) log.Info().Str("foo", "bar").Msg("") + log.Debug().Str("foo", "bar").Msg("") // Should not be logged. assert.Equal(t, fillZerologTestFields(t, "{\"level\":\"info\",\"time\":\"%s\",\"source\":\"%s\",\"foo\":\"bar\"}\n"), out.String()) }) @@ -103,6 +108,7 @@ func TestInfo(t *testing.T) { out := &bytes.Buffer{} log := New(Slog, t.Name(), out) log.Info().Str("foo", "bar").Msg("") + log.Debug().Str("foo", "bar").Msg("") // Should not be logged. assert.Equal(t, fillSlogTestFields(t, "level=INFO msg=\"\" source=%s foo=bar\n"), out.String()) }) }) @@ -115,6 +121,10 @@ func TestInfo(t *testing.T) { Str("foo", "bar"). Int("n", 123). Msg("") + log.Debug(). // Should not be logged. + Str("foo", "bar"). + Int("n", 123). + Msg("") assert.Equal(t, "", out.String()) }) @@ -125,6 +135,10 @@ func TestInfo(t *testing.T) { Str("foo", "bar"). Int("n", 123). Msg("") + log.Debug(). // Should not be logged. + Str("foo", "bar"). + Int("n", 123). + Msg("") assert.Equal(t, fillZerologTestFields(t, "{\"level\":\"info\",\"time\":\"%s\",\"source\":\"%s\",\"foo\":\"bar\",\"n\":123}\n"), out.String()) }) @@ -135,6 +149,10 @@ func TestInfo(t *testing.T) { Str("foo", "bar"). Int("n", 123). Msg("") + log.Debug(). // Should not be logged. + Str("foo", "bar"). + Int("n", 123). + Msg("") assert.Equal(t, fillSlogTestFields(t, "level=INFO msg=\"\" source=%s foo=bar n=123\n"), out.String()) }) })