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

Map V-level logging to logrus named logging #13

Merged
merged 1 commit into from
May 1, 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
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