Skip to content

Commit

Permalink
logging: use klog.Background
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer [email protected]
  • Loading branch information
sbueringer committed Jun 7, 2022
1 parent 82ee8f0 commit 4b0a504
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 29 deletions.
8 changes: 2 additions & 6 deletions bootstrap/kubeadm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"k8s.io/component-base/logs"
_ "k8s.io/component-base/logs/json/register"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -150,11 +149,8 @@ func main() {
os.Exit(1)
}

// Set the Klog format, as the Serialize format shouldn't be used anymore.
// This makes sure that the logs are formatted correctly, i.e.:
// * JSON logging format: msg isn't serialized twice
// * text logging format: values are formatted with their .String() func.
ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)))
// klog.Background will automatically use the right logger.
ctrl.SetLogger(klog.Background())

if profilerAddress != "" {
klog.Infof("Profiler listening for requests at %s", profilerAddress)
Expand Down
8 changes: 2 additions & 6 deletions controlplane/kubeadm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"k8s.io/component-base/logs"
_ "k8s.io/component-base/logs/json/register"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -153,11 +152,8 @@ func main() {
os.Exit(1)
}

// Set the Klog format, as the Serialize format shouldn't be used anymore.
// This makes sure that the logs are formatted correctly, i.e.:
// * JSON logging format: msg isn't serialized twice
// * text logging format: values are formatted with their .String() func.
ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)))
// klog.Background will automatically use the right logger.
ctrl.SetLogger(klog.Background())

if profilerAddress != "" {
klog.Infof("Profiler listening for requests at %s", profilerAddress)
Expand Down
7 changes: 2 additions & 5 deletions docs/book/src/developer/providers/v1.1-to-v1.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ in ClusterAPI are kept in sync with the versions used by `sigs.k8s.io/controller
+ os.Exit(1)
+ }
+
+ // Set the Klog format, as the Serialize format shouldn't be used anymore.
+ // This makes sure that the logs are formatted correctly, i.e.:
+ // * JSON logging format: msg isn't serialized twice
+ // * text logging format: values are formatted with their .String() func.
+ ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)))
+ // klog.Background will automatically use the right logger.
+ ctrl.SetLogger(klog.Background())
- ctrl.SetLogger(klogr.New())
```
</details>
Expand Down
8 changes: 2 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"k8s.io/component-base/logs"
_ "k8s.io/component-base/logs/json/register"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -208,11 +207,8 @@ func main() {
os.Exit(1)
}

// Set the Klog format, as the Serialize format shouldn't be used anymore.
// This makes sure that the logs are formatted correctly, i.e.:
// * JSON logging format: msg isn't serialized twice
// * text logging format: values are formatted with their .String() func.
ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)))
// klog.Background will automatically use the right logger.
ctrl.SetLogger(klog.Background())

if profilerAddress != "" {
klog.Infof("Profiler listening for requests at %s", profilerAddress)
Expand Down
8 changes: 2 additions & 6 deletions test/infrastructure/docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"k8s.io/component-base/logs"
_ "k8s.io/component-base/logs/json/register"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"

Expand Down Expand Up @@ -123,11 +122,8 @@ func main() {
os.Exit(1)
}

// Set the Klog format, as the Serialize format shouldn't be used anymore.
// This makes sure that the logs are formatted correctly, i.e.:
// * JSON logging format: msg isn't serialized twice
// * text logging format: values are formatted with their .String() func.
ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)))
// klog.Background will automatically use the right logger.
ctrl.SetLogger(klog.Background())

if profilerAddress != "" {
klog.Infof("Profiler listening for requests at %s", profilerAddress)
Expand Down

0 comments on commit 4b0a504

Please sign in to comment.