Skip to content

Commit

Permalink
Merge pull request #15 from bombsimon/simplify-log-call
Browse files Browse the repository at this point in the history
Simplify severity logging
  • Loading branch information
bombsimon authored May 2, 2022
2 parents 876a31e + 0e60174 commit 2a0d8cd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
7 changes: 6 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@ func main() {
logrusLog,
logrusr.WithReportCaller(),
).WithCallDepth(0)
log.V(2).Info("NOW you should see this")

log.V(0).Info("you should see this as info")
log.V(1).Info("you should see this as debug")
log.V(2).Info("you should see this as trace")
log.V(1).V(1).Info("you should see this as trace")
log.V(10).Info("you should not see this")
}
12 changes: 3 additions & 9 deletions logrusr.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,9 @@ func (l *logrusr) Info(level int, msg string, keysAndValues ...interface{}) {
log = log.WithField("caller", c)
}

log = log.WithFields(listToLogrusFields(l.defaultFormatter, keysAndValues...))

if level <= 0 {
log.Info(msg)
} else if level == 1 {
log.Debug(msg)
} else {
log.Trace(msg)
}
log.
WithFields(listToLogrusFields(l.defaultFormatter, keysAndValues...)).
Log(logrus.Level(level+logrusDiffToInfo), msg)
}

// Error logs error messages. Since the log will be written with `Error` level
Expand Down
32 changes: 32 additions & 0 deletions logrusr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,38 @@ func TestLogging(t *testing.T) {
"msg": "hello, world",
},
},
{
description: "negative V-logging truncates to info",
logrusLogger: func() logrus.FieldLogger {
l := logrus.New()
l.SetLevel(logrus.TraceLevel)

return l
},
logFunc: func(log logr.Logger) {
log.V(-10).Info("hello, world")
},
assertions: map[string]string{
"level": "info",
"msg": "hello, world",
},
},
{
description: "addative V-logging, negatives ignored",
logrusLogger: func() logrus.FieldLogger {
l := logrus.New()
l.SetLevel(logrus.TraceLevel)

return l
},
logFunc: func(log logr.Logger) {
log.V(0).V(1).V(-20).V(1).Info("hello, world")
},
assertions: map[string]string{
"level": "trace",
"msg": "hello, world",
},
},
{
description: "arguments are added while calling Info()",
logrusLogger: func() logrus.FieldLogger {
Expand Down

0 comments on commit 2a0d8cd

Please sign in to comment.