Skip to content

Commit

Permalink
Map V-level logging to logrus named logging
Browse files Browse the repository at this point in the history
After looking at the code for other logr implementations such as zapr
and zerologr it makes more sens that logrus also should try to map logr
V-level logging to named logging as close as possible.

* V0 is kept as severity Info
* V1 is logged as severity Debug
* V2 is logged as severity Trace
  • Loading branch information
bombsimon committed May 1, 2022
1 parent 97f0772 commit 9f3fd50
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 9f3fd50

Please sign in to comment.