Skip to content

Commit d5c3ebb

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 d5c3ebb

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
type NullLogger struct{}
12+
13+
var _ logr.Logger = NullLogger{}
14+
15+
func (_ NullLogger) Info(_ string, _ ...interface{}) {
16+
// Do nothing.
17+
}
18+
19+
func (_ NullLogger) Enabled() bool {
20+
return false
21+
}
22+
23+
func (_ NullLogger) Error(_ error, _ string, _ ...interface{}) {
24+
// Do nothing.
25+
}
26+
27+
func (log NullLogger) V(_ int) logr.InfoLogger {
28+
return log
29+
}
30+
31+
func (log NullLogger) WithName(_ string) logr.Logger {
32+
return log
33+
}
34+
35+
func (log NullLogger) WithValues(_ ...interface{}) logr.Logger {
36+
return log
37+
}
38+
39+

0 commit comments

Comments
 (0)