From 2a4434f5f1f3c8101aee0335b92b97413f4f495c Mon Sep 17 00:00:00 2001 From: Arpad Kiss <158226216+arp-est@users.noreply.github.com> Date: Sat, 5 Oct 2024 01:07:12 +0200 Subject: [PATCH] Add boundary check to SetupLogLevelChange (#1675) Signed-off-by: Arpad Kiss --- pkg/tools/log/logruslogger/levelchange.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/tools/log/logruslogger/levelchange.go b/pkg/tools/log/logruslogger/levelchange.go index 6852cfebe..e607238c5 100644 --- a/pkg/tools/log/logruslogger/levelchange.go +++ b/pkg/tools/log/logruslogger/levelchange.go @@ -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) @@ -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) } }