Skip to content

Commit

Permalink
Add boundary check to SetupLogLevelChange (#1675)
Browse files Browse the repository at this point in the history
Signed-off-by: Arpad Kiss <arpad.a.kiss@est.tech>
  • Loading branch information
arp-est authored Oct 4, 2024
1 parent 5a9d3ce commit 95c7ff7
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/tools/log/logruslogger/levelchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ import (

// SetupLevelChangeOnSignal sets the loglevel to the one specified in the map when a signal assotiated to it arrives
func SetupLevelChangeOnSignal(ctx context.Context, signals map[os.Signal]logrus.Level) {
var currentLevelCount int
for _, v := range signals {
if v == logrus.GetLevel() {
currentLevelCount++
}
}
if currentLevelCount == len(signals) {
log.FromContext(ctx).WithField("logruslogger", "SetupLevelChangeOnSignal").Warn("Detected that log level will never change, disabling loglevel change on signal")
return
}
sigChannel := make(chan os.Signal, len(signals))
for sig := range signals {
signal.Notify(sigChannel, sig)
Expand All @@ -41,7 +51,7 @@ func SetupLevelChangeOnSignal(ctx context.Context, signals map[os.Signal]logrus.
return
case sig := <-sigChannel:
lvl := signals[sig]
log.FromContext(ctx).Infof("Setting log level to '%s'", lvl.String())
log.FromContext(ctx).WithField("logruslogger", "SetupLevelChangeOnSignal").Infof("Setting log level to '%s'", lvl.String())
logrus.SetLevel(lvl)
}
}
Expand Down

0 comments on commit 95c7ff7

Please sign in to comment.