Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify severity logging #15

Merged
merged 1 commit into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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