Skip to content

Commit b889d26

Browse files
committed
silence the 'log.SetLogger(...) was never called' error
Signed-off-by: chaosi-zju <[email protected]>
1 parent 1255a08 commit b889d26

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

cmd/agent/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"k8s.io/component-base/cli"
2323
_ "k8s.io/component-base/logs/json/register" // for JSON log format registration
24+
"k8s.io/klog/v2"
2425
controllerruntime "sigs.k8s.io/controller-runtime"
2526
_ "sigs.k8s.io/controller-runtime/pkg/metrics"
2627

@@ -29,6 +30,12 @@ import (
2930

3031
func main() {
3132
ctx := controllerruntime.SetupSignalHandler()
33+
// Starting from version 0.15.0, controller-runtime expects its consumers to set a logger through log.SetLogger.
34+
// If SetLogger is not called within the first 30 seconds of a binaries lifetime, it will get
35+
// set to a NullLogSink and report an error. Here's to silence the "log.SetLogger(...) was never called; logs will not be displayed" error
36+
// by setting a logger through log.SetLogger.
37+
// More info refer to: https://github.com/karmada-io/karmada/pull/4885.
38+
controllerruntime.SetLogger(klog.Background())
3239
cmd := app.NewAgentCommand(ctx)
3340
code := cli.Run(cmd)
3441
os.Exit(code)

operator/cmd/operator/operator.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"k8s.io/component-base/cli"
2323
_ "k8s.io/component-base/logs/json/register" // for JSON log format registration
24+
"k8s.io/klog/v2"
2425
controllerruntime "sigs.k8s.io/controller-runtime"
2526
_ "sigs.k8s.io/controller-runtime/pkg/metrics"
2627

@@ -29,6 +30,12 @@ import (
2930

3031
func main() {
3132
ctx := controllerruntime.SetupSignalHandler()
33+
// Starting from version 0.15.0, controller-runtime expects its consumers to set a logger through log.SetLogger.
34+
// If SetLogger is not called within the first 30 seconds of a binaries lifetime, it will get
35+
// set to a NullLogSink and report an error. Here's to silence the "log.SetLogger(...) was never called; logs will not be displayed" error
36+
// by setting a logger through log.SetLogger.
37+
// More info refer to: https://github.com/karmada-io/karmada/pull/4885.
38+
controllerruntime.SetLogger(klog.Background())
3239
command := app.NewOperatorCommand(ctx)
3340
code := cli.Run(command)
3441
os.Exit(code)

0 commit comments

Comments
 (0)