Skip to content

Commit

Permalink
fix: LoggerFactory now emits loggers at the correct loglevel
Browse files Browse the repository at this point in the history
  • Loading branch information
rg0now committed Dec 4, 2023
1 parent e61cfb2 commit a92d3f7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ func (f *RateLimitedLoggerFactory) NewLogger(scope string) logging.LeveledLogger
// disable rate-limiting logging at lower loglevels
l := f.DefaultLogLevel

// this will never happen: loggers are always created with the default loglevel
scopeLevel, found := f.ScopeLevels[scope]
if found {
l = scopeLevel
Expand Down Expand Up @@ -184,7 +183,13 @@ func (f *LeveledLoggerFactory) newLogger(scope string, limit rate.Limit, burst i
return logger
}

l := NewRateLimitedLoggerForScope(scope, f.DefaultLogLevel, f.Writer, limit, burst)
logLevel := f.DefaultLogLevel
scopeLevel, found := f.ScopeLevels[scope]
if found {
logLevel = scopeLevel
}

l := NewRateLimitedLoggerForScope(scope, logLevel, f.Writer, limit, burst)

l.DefaultLeveledLogger.
WithTraceLogger(log.New(l.RateLimitedWriter, fmt.Sprintf("%s TRACE: ", scope), defaultFlags)).
Expand Down
38 changes: 38 additions & 0 deletions pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,44 @@ var loggerTests = []loggerTestCase{
log.Debug("dummy")
assert.Zerof(t, loglenr(), "DEBUG for level %s", level)

log.Trace("dummy")
assert.Zerof(t, loglenr(), "TRACE for level %s", level)
},
},
{
name: "set-loglevel-for-newly-created-logger",
defaultLogLevel: "all:TRACE",
scopeLogLevel: "TRACE",
prep: func(lf LoggerFactory) {
lf.SetLevel("all:error,new-scope:DEBUG")
},
tester: func(t *testing.T, lf LoggerFactory) {
level := lf.GetLevel("all")
assert.Equal(t, "Error", level, "default scope: level")

level = lf.GetLevel(testScope)
assert.Equal(t, "Error", level, "dummy scope: level")

level = lf.GetLevel("new-scope")
assert.Equal(t, "Debug", level, "new scope: level")

log := lf.NewLogger("new-scope")

level = lf.GetLevel("new-scope")
assert.Equal(t, "Debug", level, "new scope: level")

log.Error("dummy")
assert.Containsf(t, logreadr(), "dummy", "ERROR for level %s", level)

log.Warn("dummy")
assert.Containsf(t, logreadr(), "dummy", "WARN for level %s", level)

log.Info("dummy")
assert.Containsf(t, logreadr(), "dummy", "INFO for level %s", level)

log.Debug("dummy")
assert.Containsf(t, logreadr(), "dummy", "DEBUG for level %s", level)

log.Trace("dummy")
assert.Zerof(t, loglenr(), "TRACE for level %s", level)
},
Expand Down

0 comments on commit a92d3f7

Please sign in to comment.