Skip to content

Commit

Permalink
Use klog as controller-runtime default logger
Browse files Browse the repository at this point in the history
  • Loading branch information
astraw99 committed Oct 17, 2022
1 parent 8ad090e commit e5c184c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
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()
)

// 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

0 comments on commit e5c184c

Please sign in to comment.