From e06ac3313c6f486ec920b9a434ed6a253e1e1d87 Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Wed, 31 May 2023 14:51:43 -0700 Subject: [PATCH] :bug: SetLogger should work with nil sinks Signed-off-by: Vince Prignano --- pkg/log/deleg.go | 3 +++ pkg/log/log_test.go | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/pkg/log/deleg.go b/pkg/log/deleg.go index c27b4305f8..6eb551d3b6 100644 --- a/pkg/log/deleg.go +++ b/pkg/log/deleg.go @@ -188,6 +188,9 @@ func (l *delegatingLogSink) WithValues(tags ...interface{}) logr.LogSink { // provided, instead of the temporary initial one, if this method // has not been previously called. func (l *delegatingLogSink) Fulfill(actual logr.LogSink) { + if actual == nil { + actual = NullLogSink{} + } if l.promise != nil { l.promise.Fulfill(actual) } diff --git a/pkg/log/log_test.go b/pkg/log/log_test.go index c0ca462369..b75604b6be 100644 --- a/pkg/log/log_test.go +++ b/pkg/log/log_test.go @@ -286,6 +286,14 @@ var _ = Describe("logging", func() { logInfo{msg: "msg 2"}, )) }) + + It("should handle nil sinks", func() { + By("fulfilling once") + delegLog.Fulfill(logr.Discard().GetSink()) + By("grabbing a sub-logger and logging") + l1 := logr.New(delegLog).WithName("nilsink").WithValues("newtag", "newvalue2") + l1.Info("test") + }) }) Describe("logger from context", func() {