From e5c184c2c1c98cdd7f75b1ad25a08a513440557d Mon Sep 17 00:00:00 2001 From: astraw99 Date: Sun, 16 Oct 2022 23:54:00 +0800 Subject: [PATCH] Use klog as controller-runtime default logger --- pkg/log/log.go | 5 ++++- pkg/log/log_test.go | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/log/log.go b/pkg/log/log.go index 082dce3adb..6c01daea20 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -38,6 +38,8 @@ import ( "sync" "time" + "k8s.io/klog/v2" + "github.com/go-logr/logr" ) @@ -81,7 +83,8 @@ var ( // set to a NullLogSink. var ( dlog = NewDelegatingLogSink(NullLogSink{}) - Log = logr.New(dlog) + // Log uses `klog` as the controller-runtime default logger. + Log = klog.NewKlogr() ) // FromContext returns a logger with predefined values from a context.Context. diff --git a/pkg/log/log_test.go b/pkg/log/log_test.go index a4f4d895ab..3beb4012e3 100644 --- a/pkg/log/log_test.go +++ b/pkg/log/log_test.go @@ -100,7 +100,8 @@ var _ = Describe("logging", func() { Describe("top-level logger", func() { It("hold newly created loggers until a logger is set", func() { By("grabbing a new sub-logger and logging to it") - l1 := Log.WithName("runtimeLog").WithValues("newtag", "newvalue1") + lgr := logr.New(dlog) + l1 := lgr.WithName("runtimeLog").WithValues("newtag", "newvalue1") l1.Info("before msg") By("actually setting the logger") @@ -108,7 +109,7 @@ var _ = Describe("logging", func() { SetLogger(logr.New(logger)) By("grabbing another sub-logger and logging to both loggers") - l2 := Log.WithName("runtimeLog").WithValues("newtag", "newvalue2") + l2 := lgr.WithName("runtimeLog").WithValues("newtag", "newvalue2") l1.Info("after msg 1") l2.Info("after msg 2")