Skip to content

Commit

Permalink
Merge pull request #13 from bombsimon/logrus-severity
Browse files Browse the repository at this point in the history
Map V-level logging to logrus named logging
  • Loading branch information
bombsimon authored May 1, 2022
2 parents 97f0772 + 9f3fd50 commit bf8a48a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
12 changes: 9 additions & 3 deletions logrusr.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,15 @@ func (l *logrusr) Info(level int, msg string, keysAndValues ...interface{}) {
l.logger = l.logger.WithField("caller", c)
}

l.logger.
WithFields(listToLogrusFields(l.defaultFormatter, keysAndValues...)).
Info(msg)
log := l.logger.WithFields(listToLogrusFields(l.defaultFormatter, keysAndValues...))

if level <= 0 {
log.Info(msg)
} else if level == 1 {
log.Debug(msg)
} else {
log.Trace(msg)
}
}

// Error logs error messages. Since the log will be written with `Error` level
Expand Down
19 changes: 18 additions & 1 deletion logrusr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,27 @@ func TestLogging(t *testing.T) {
return logrus.New()
},
logFunc: func(log logr.Logger) {
log.V(1).Info("hello, world")
log.V(2).Info("hello, world")
},
assertions: nil,
},
{
description: "V(1) logging with debug level set is shown",
logrusLogger: func() logrus.FieldLogger {
l := logrus.New()
l.SetLevel(logrus.TraceLevel)

return l
},
logFunc: func(log logr.Logger) {
log.V(1).Info("hello, world")
},
assertions: map[string]string{
"level": "debug",
"msg": "hello, world",
},
},
{
description: "V(2) logging with trace level set is shown",
logrusLogger: func() logrus.FieldLogger {
Expand All @@ -103,7 +120,7 @@ func TestLogging(t *testing.T) {
log.V(2).Info("hello, world")
},
assertions: map[string]string{
"level": "info",
"level": "trace",
"msg": "hello, world",
},
},
Expand Down

0 comments on commit bf8a48a

Please sign in to comment.