Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Use klog as the controller-runtime default logger #2021

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
"sync"
"time"

"k8s.io/klog/v2"

"github.com/go-logr/logr"
)

Expand Down Expand Up @@ -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()
Copy link
Member

@alvaroaleman alvaroaleman Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change. Controller-Runtime doesn't set any logger by default and allows people to use whatever logger they like for as long as it implements the interface. We want to keep offering that.

Please just set your desired logger at the start of your program.
/hold

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, raised a new PR kubernetes-sigs/kubebuilder#3033 to set the default logger.
PTAL thanks.

)

// FromContext returns a logger with predefined values from a context.Context.
Expand Down
5 changes: 3 additions & 2 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,16 @@ 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")
logger := &fakeLogger{root: &fakeLoggerRoot{}}
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")

Expand Down