Skip to content

Commit 434aee9

Browse files
committed
Avoid adding test flags when using the null logger
The null logger from logr is in the same package as the testing logger, which means it accidentally adds testing flags to all binaries that import it. Copy the null logger into the pkg/runtime/log package to avoid this.
1 parent f5b79b9 commit 434aee9

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

pkg/runtime/log/log.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"log"
77

88
"github.com/go-logr/logr"
9-
tlogr "github.com/go-logr/logr/testing"
109
"github.com/go-logr/zapr"
1110
"go.uber.org/zap"
1211
)
@@ -45,7 +44,7 @@ func SetLogger(l logr.Logger) {
4544
// to another logr.Logger. You *must* call SetLogger to
4645
// get any actual logging.
4746
var Log = &DelegatingLogger{
48-
Logger: tlogr.NullLogger{},
47+
Logger: NullLogger{},
4948
promise: &loggerPromise{},
5049
}
5150

pkg/runtime/log/null.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package log
2+
3+
import (
4+
"github.com/go-logr/logr"
5+
)
6+
7+
// NB: this is the same as the null logger logr/testing,
8+
// but avoids accidentally adding the testing flags to
9+
// all binaries.
10+
11+
// NullLogger is a logr.Logger that does nothing.
12+
type NullLogger struct{}
13+
14+
var _ logr.Logger = NullLogger{}
15+
16+
// Info implements logr.InfoLogger
17+
func (NullLogger) Info(_ string, _ ...interface{}) {
18+
// Do nothing.
19+
}
20+
21+
// Enabled implements logr.InfoLogger
22+
func (NullLogger) Enabled() bool {
23+
return false
24+
}
25+
26+
// Error implements logr.Logger
27+
func (NullLogger) Error(_ error, _ string, _ ...interface{}) {
28+
// Do nothing.
29+
}
30+
31+
// V implements logr.Logger
32+
func (log NullLogger) V(_ int) logr.InfoLogger {
33+
return log
34+
}
35+
36+
// WithName implements logr.Logger
37+
func (log NullLogger) WithName(_ string) logr.Logger {
38+
return log
39+
}
40+
41+
// WithValues implements logr.Logger
42+
func (log NullLogger) WithValues(_ ...interface{}) logr.Logger {
43+
return log
44+
}

0 commit comments

Comments
 (0)