Skip to content

Commit 2a0d8cd

Browse files
authored
Merge pull request #15 from bombsimon/simplify-log-call
Simplify severity logging
2 parents 876a31e + 0e60174 commit 2a0d8cd

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

example/main.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,10 @@ func main() {
4949
logrusLog,
5050
logrusr.WithReportCaller(),
5151
).WithCallDepth(0)
52-
log.V(2).Info("NOW you should see this")
52+
53+
log.V(0).Info("you should see this as info")
54+
log.V(1).Info("you should see this as debug")
55+
log.V(2).Info("you should see this as trace")
56+
log.V(1).V(1).Info("you should see this as trace")
57+
log.V(10).Info("you should not see this")
5358
}

logrusr.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,9 @@ func (l *logrusr) Info(level int, msg string, keysAndValues ...interface{}) {
103103
log = log.WithField("caller", c)
104104
}
105105

106-
log = log.WithFields(listToLogrusFields(l.defaultFormatter, keysAndValues...))
107-
108-
if level <= 0 {
109-
log.Info(msg)
110-
} else if level == 1 {
111-
log.Debug(msg)
112-
} else {
113-
log.Trace(msg)
114-
}
106+
log.
107+
WithFields(listToLogrusFields(l.defaultFormatter, keysAndValues...)).
108+
Log(logrus.Level(level+logrusDiffToInfo), msg)
115109
}
116110

117111
// Error logs error messages. Since the log will be written with `Error` level

logrusr_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,38 @@ func TestLogging(t *testing.T) {
124124
"msg": "hello, world",
125125
},
126126
},
127+
{
128+
description: "negative V-logging truncates to info",
129+
logrusLogger: func() logrus.FieldLogger {
130+
l := logrus.New()
131+
l.SetLevel(logrus.TraceLevel)
132+
133+
return l
134+
},
135+
logFunc: func(log logr.Logger) {
136+
log.V(-10).Info("hello, world")
137+
},
138+
assertions: map[string]string{
139+
"level": "info",
140+
"msg": "hello, world",
141+
},
142+
},
143+
{
144+
description: "addative V-logging, negatives ignored",
145+
logrusLogger: func() logrus.FieldLogger {
146+
l := logrus.New()
147+
l.SetLevel(logrus.TraceLevel)
148+
149+
return l
150+
},
151+
logFunc: func(log logr.Logger) {
152+
log.V(0).V(1).V(-20).V(1).Info("hello, world")
153+
},
154+
assertions: map[string]string{
155+
"level": "trace",
156+
"msg": "hello, world",
157+
},
158+
},
127159
{
128160
description: "arguments are added while calling Info()",
129161
logrusLogger: func() logrus.FieldLogger {

0 commit comments

Comments
 (0)