forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support SIGUSR1 log level incrementing (sensu#3685)
This commit adds support to sensu-backend and sensu-agent for incrementing the log level with SIGUSR1. When the process receives this signal, the log level will be incremented in terms of verbosity. When the level is at TRACE, the most verbose log level, and the log level is incremented, it will wrap around to error level. Known issues: SIGUSR1 is not supported on Windows. Signed-off-by: Eric Chlebek <[email protected]>
- Loading branch information
Showing
7 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// +build !windows | ||
|
||
package cmd | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/sensu/sensu-go/util/logging" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func init() { | ||
sigs := make(chan os.Signal, 1) | ||
signal.Notify(sigs, syscall.SIGUSR1) | ||
go func() { | ||
for range sigs { | ||
level := logrus.GetLevel() | ||
newLevel := logging.IncrementLogLevel(level) | ||
logrus.Warnf("set log level to %s", newLevel) | ||
logrus.SetLevel(newLevel) | ||
if newLevel == logrus.WarnLevel { | ||
// repeat the log call, as it wouldn't have been logged at | ||
// error level. | ||
logrus.Warnf("set log level to %s", newLevel) | ||
} | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// +build !windows | ||
|
||
package cmd | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/sensu/sensu-go/util/logging" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func init() { | ||
sigs := make(chan os.Signal, 1) | ||
signal.Notify(sigs, syscall.SIGUSR1) | ||
go func() { | ||
for range sigs { | ||
level := logrus.GetLevel() | ||
newLevel := logging.IncrementLogLevel(level) | ||
logrus.Warnf("set log level to %s", newLevel) | ||
logrus.SetLevel(newLevel) | ||
if newLevel == logrus.WarnLevel { | ||
// repeat the log call, as it wouldn't have been logged at | ||
// error level. | ||
logrus.Warnf("set log level to %s", newLevel) | ||
} | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package logging | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func TestIncrementLogLevel(t *testing.T) { | ||
tests := []struct { | ||
Level logrus.Level | ||
ExpLevel logrus.Level | ||
}{ | ||
{ | ||
Level: logrus.ErrorLevel, | ||
ExpLevel: logrus.WarnLevel, | ||
}, | ||
{ | ||
Level: logrus.WarnLevel, | ||
ExpLevel: logrus.InfoLevel, | ||
}, | ||
{ | ||
Level: logrus.InfoLevel, | ||
ExpLevel: logrus.DebugLevel, | ||
}, | ||
{ | ||
Level: logrus.DebugLevel, | ||
ExpLevel: logrus.TraceLevel, | ||
}, | ||
{ | ||
Level: logrus.TraceLevel, | ||
ExpLevel: logrus.ErrorLevel, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.Level.String(), func(t *testing.T) { | ||
if got, want := IncrementLogLevel(test.Level), test.ExpLevel; got != want { | ||
t.Fatalf("bad log level: got %s, want %s", got, want) | ||
} | ||
}) | ||
} | ||
} |