Skip to content

Commit

Permalink
Support SIGUSR1 log level incrementing (sensu#3685)
Browse files Browse the repository at this point in the history
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
echlebek authored Apr 15, 2020
1 parent 3a44283 commit 4a69ad0
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Versioning](http://semver.org/spec/v2.0.0.html).
- [Web] Added the ability for labels and annotations with links to images to be
displayed inline.
- [Web] Added additional modes for those with colour blindness.
- Users can now increment the logging level by sending SIGUSR1 to the
sensu-backend or sensu-agent process.
- Added a new `sensuctl describe-type` command to list all resource types.

### Changed
Expand Down
4 changes: 3 additions & 1 deletion agent/cmd/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import "github.com/sirupsen/logrus"
import (
"github.com/sirupsen/logrus"
)

var logger = logrus.WithFields(logrus.Fields{
"component": "cmd",
Expand Down
30 changes: 30 additions & 0 deletions agent/cmd/logger_unix.go
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)
}
}
}()
}
4 changes: 3 additions & 1 deletion backend/cmd/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import "github.com/sirupsen/logrus"
import (
"github.com/sirupsen/logrus"
)

var logger = logrus.WithFields(logrus.Fields{
"component": "cmd",
Expand Down
30 changes: 30 additions & 0 deletions backend/cmd/logger_unix.go
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)
}
}
}()
}
10 changes: 10 additions & 0 deletions util/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,13 @@ func EventFields(event *types.Event, debug bool) map[string]interface{} {

return fields
}

// IncrementLogLevel increments the log level, or wraps around to "error" level.
func IncrementLogLevel(level logrus.Level) logrus.Level {
level++
if level > logrus.TraceLevel {
// wrap around to error level
level = logrus.ErrorLevel
}
return level
}
42 changes: 42 additions & 0 deletions util/logging/logging_test.go
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)
}
})
}
}

0 comments on commit 4a69ad0

Please sign in to comment.