Skip to content

Commit 95f7e32

Browse files
committed
⚠ pkg/log: Default to NullLogger instead of DelegatingLogger
The DelegatingLogger used by default causes a high number of memory allocations for controllers when it doesn't get populated. It does so in a completely invisible manner, because "not populated" also means that its not going to log anything. IMHO it is reasonable to expect consumers to set a Logger before attempting to use it.
1 parent cf7ac88 commit 95f7e32

File tree

2 files changed

+2
-25
lines changed

2 files changed

+2
-25
lines changed

pkg/log/log.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ import (
4141

4242
// SetLogger sets a concrete logging implementation for all deferred Loggers.
4343
func SetLogger(l logr.Logger) {
44-
Log.Fulfill(l)
44+
Log = l
4545
}
4646

4747
// Log is the base logger used by kubebuilder. It delegates
4848
// to another logr.Logger. You *must* call SetLogger to
4949
// get any actual logging.
50-
var Log = NewDelegatingLogger(NullLogger{})
50+
var Log logr.Logger = NullLogger{}
5151

5252
// FromContext returns a logger with predefined values from a context.Context.
5353
func FromContext(ctx context.Context, keysAndValues ...interface{}) logr.Logger {

pkg/log/log_test.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,6 @@ func (f *fakeLogger) V(lvl int) logr.InfoLogger { return f }
9292

9393
var _ = Describe("logging", func() {
9494

95-
Describe("top-level logger", func() {
96-
It("hold newly created loggers until a logger is set", func() {
97-
By("grabbing a new sub-logger and logging to it")
98-
l1 := Log.WithName("runtimeLog").WithValues("newtag", "newvalue1")
99-
l1.Info("before msg")
100-
101-
By("actually setting the logger")
102-
logger := &fakeLogger{root: &fakeLoggerRoot{}}
103-
SetLogger(logger)
104-
105-
By("grabbing another sub-logger and logging to both loggers")
106-
l2 := Log.WithName("runtimeLog").WithValues("newtag", "newvalue2")
107-
l1.Info("after msg 1")
108-
l2.Info("after msg 2")
109-
110-
By("ensuring that messages after the logger was set were logged")
111-
Expect(logger.root.messages).To(ConsistOf(
112-
logInfo{name: []string{"runtimeLog"}, tags: []interface{}{"newtag", "newvalue1"}, msg: "after msg 1"},
113-
logInfo{name: []string{"runtimeLog"}, tags: []interface{}{"newtag", "newvalue2"}, msg: "after msg 2"},
114-
))
115-
})
116-
})
117-
11895
Describe("lazy logger initialization", func() {
11996
var (
12097
root *fakeLoggerRoot

0 commit comments

Comments
 (0)