Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add boundary check to SetupLogLevelChange #1675

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading