Skip to content

Commit

Permalink
Use NGF logger in client-go library (#2618)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sjberman authored Sep 27, 2024
1 parent 900c877 commit 433eba2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cmd/gateway/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/log"
ctlrZap "sigs.k8s.io/controller-runtime/pkg/log/zap"

Expand Down Expand Up @@ -125,6 +126,8 @@ func createStaticModeCommand() *cobra.Command {
atom := zap.NewAtomicLevel()

logger := ctlrZap.New(ctlrZap.Level(atom))
klog.SetLogger(logger)

commit, date, dirty := getBuildInfo()
logger.Info(
"Starting NGINX Gateway Fabric in static mode",
Expand Down
1 change: 1 addition & 0 deletions docs/developer/logging-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ There are two critical libraries for NGF that log:
of that project.
- [client-go](https://github.com/kubernetes/client-go).
- It uses [klog](https://github.com/kubernetes/klog) for logging.
- We inject the above logger into klog to ensure it uses the same formatting.
- Most of the logging is done at increased klog-specific
verbosity. However, errors are logged at the default verbosity like
in [this line](https://github.com/kubernetes/client-go/blob/c5b1c13ccbedeb03c00ba162ef27566b0dfb512d/tools/record/event.go#L240).
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
k8s.io/apiextensions-apiserver v0.31.1
k8s.io/apimachinery v0.31.1
k8s.io/client-go v0.31.1
k8s.io/klog/v2 v2.130.1
sigs.k8s.io/controller-runtime v0.19.0
sigs.k8s.io/gateway-api v1.1.0
)
Expand Down Expand Up @@ -89,7 +90,6 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240423202451-8948a665c108 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down

0 comments on commit 433eba2

Please sign in to comment.