Skip to content

Commit f632e04

Browse files
sjbermansalonichf5
authored andcommitted
Use NGF logger in client-go library (#2618)
Problem: The klog logger used by client-go has different formatting than the NGF loggers. Solution: Set the klog logger to wrap the NGF logger and therefore use the same formatting.
1 parent 6c59335 commit f632e04

File tree

3 files changed

+5
-1
lines changed

3 files changed

+5
-1
lines changed

cmd/gateway/commands.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"go.uber.org/zap"
1414
"k8s.io/apimachinery/pkg/types"
1515
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
16+
"k8s.io/klog/v2"
1617
"sigs.k8s.io/controller-runtime/pkg/log"
1718
ctlrZap "sigs.k8s.io/controller-runtime/pkg/log/zap"
1819

@@ -128,6 +129,8 @@ func createStaticModeCommand() *cobra.Command {
128129
atom := zap.NewAtomicLevel()
129130

130131
logger := ctlrZap.New(ctlrZap.Level(atom))
132+
klog.SetLogger(logger)
133+
131134
commit, date, dirty := getBuildInfo()
132135
logger.Info(
133136
"Starting NGINX Gateway Fabric in static mode",

docs/developer/logging-guidelines.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ There are two critical libraries for NGF that log:
383383
of that project.
384384
- [client-go](https://github.com/kubernetes/client-go).
385385
- It uses [klog](https://github.com/kubernetes/klog) for logging.
386+
- We inject the above logger into klog to ensure it uses the same formatting.
386387
- Most of the logging is done at increased klog-specific
387388
verbosity. However, errors are logged at the default verbosity like
388389
in [this line](https://github.com/kubernetes/client-go/blob/c5b1c13ccbedeb03c00ba162ef27566b0dfb512d/tools/record/event.go#L240).

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
k8s.io/apiextensions-apiserver v0.31.1
2424
k8s.io/apimachinery v0.31.1
2525
k8s.io/client-go v0.31.1
26+
k8s.io/klog/v2 v2.130.1
2627
sigs.k8s.io/controller-runtime v0.19.0
2728
sigs.k8s.io/gateway-api v1.1.0
2829
)
@@ -89,7 +90,6 @@ require (
8990
gopkg.in/inf.v0 v0.9.1 // indirect
9091
gopkg.in/yaml.v2 v2.4.0 // indirect
9192
gopkg.in/yaml.v3 v3.0.1 // indirect
92-
k8s.io/klog/v2 v2.130.1 // indirect
9393
k8s.io/kube-openapi v0.0.0-20240423202451-8948a665c108 // indirect
9494
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
9595
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect

0 commit comments

Comments
 (0)