|
37 | 37 | - [Unit testing](#unit-testing)
|
38 | 38 | - [Injecting common value, logger passed through existing ctx parameter or new parameter](#injecting-common-value-logger-passed-through-existing-ctx-parameter-or-new-parameter)
|
39 | 39 | - [Resulting output](#resulting-output)
|
| 40 | + - [Integration with log/slog](#integration-with-logslog) |
40 | 41 | - [Test Plan](#test-plan)
|
41 | 42 | - [Graduation Criteria](#graduation-criteria)
|
42 | 43 | - [Alpha](#alpha)
|
|
58 | 59 | - [Propagating a logger to init code](#propagating-a-logger-to-init-code)
|
59 | 60 | - [Panic when FromContext is called before setting a logger](#panic-when-fromcontext-is-called-before-setting-a-logger)
|
60 | 61 | - [Clean separation of contextual logging and traditional klog logging](#clean-separation-of-contextual-logging-and-traditional-klog-logging)
|
| 62 | + - [Use log/slog instead of klog+logr](#use-logslog-instead-of-kloglogr) |
61 | 63 | <!-- /toc -->
|
62 | 64 |
|
63 | 65 | ## Release Signoff Checklist
|
@@ -507,6 +509,37 @@ The logcheck static code analysis tool will warn about code in Kubernetes which
|
507 | 509 | calls the underlying functions directly. Once the feature gate is no longer needed,
|
508 | 510 | a global search/replace can remove the usage of these wrapper functions again.
|
509 | 511 |
|
| 512 | +Because the feature gate is off during alpha, log calls have to repeat |
| 513 | +important key/value pairs even if those also got passed to `WithValues`: |
| 514 | + |
| 515 | +``` |
| 516 | +logger := logger.WithValues("pod", klog.KObj(pod)) |
| 517 | +... |
| 518 | +logger.Info("Processing", "pod", klog.KObj(pod)) |
| 519 | +... |
| 520 | +logger.Info("Done", "pod", klog.KObj(pod)) |
| 521 | +``` |
| 522 | + |
| 523 | +Starting with beta, the feature gate will be enabled and code can be written |
| 524 | +without such duplication to avoid the need for further changes when reaching |
| 525 | +GA: |
| 526 | + |
| 527 | +``` |
| 528 | +logger := logger.WithValues("pod", klog.KObj(pod)) |
| 529 | +... |
| 530 | +logger.Info("Processing") |
| 531 | +... |
| 532 | +logger.Info("Done") |
| 533 | +``` |
| 534 | + |
| 535 | +Documentation of APIs has to make it clear which values will always be included |
| 536 | +in log entries and thus don't need to be repeated. If in doubt, repeating them |
| 537 | +is okay: the text format will filter out duplicates if log call parameters |
| 538 | +overlap with `WithValues` parameters. For performance reasons it will not do |
| 539 | +that for duplicates between different `WithValues` calls. In JSON, repeating |
| 540 | +keys increases log volume size because there is no de-duplication, but the |
| 541 | +semantic is the same ("most recent wins"). |
| 542 | + |
510 | 543 | ### Text format
|
511 | 544 |
|
512 | 545 | The formatting and verbosity code will be moved into `internal` packages where
|
@@ -839,6 +872,15 @@ I1026 16:21:00.461886 801139 scheduler.go:464] "Status after running PostFilter
|
839 | 872 | I1026 16:21:00.461918 801139 factory.go:209] "Unable to schedule pod; no fit; waiting" pod="default/my-csi-app-inline-volume" err="0/1 nodes are available: 1 node(s) did not have enough free storage."
|
840 | 873 | ```
|
841 | 874 |
|
| 875 | +### Integration with log/slog |
| 876 | + |
| 877 | +[`log/slog`](https://pkg.go.dev/log/slog) got added in Go |
| 878 | +1.21. Interoperability with slog is [provided by |
| 879 | +logr](https://github.com/go-logr/logr/pull/222). Applications which use slog |
| 880 | +can route log output from Kubernetes packages into their `slog.Handler` and |
| 881 | +vice versa, as demonstrated with [`component-base/logs` |
| 882 | +examples](https://github.com/kubernetes/kubernetes/pull/120696). |
| 883 | + |
842 | 884 | ### Test Plan
|
843 | 885 |
|
844 | 886 | The new code will be covered by unit tests that execute as part of
|
@@ -870,7 +912,7 @@ logging.
|
870 | 912 |
|
871 | 913 | #### Beta
|
872 | 914 |
|
873 |
| -- All of kube-scheduler (in-tree) and CSI external-provisioner (out-of-tree) converted |
| 915 | +- [All of kube-controller-manager](https://github.com/kubernetes/kubernetes/pull/119250) and some [parts of kube-scheduler](https://github.com/kubernetes/kubernetes/pull/115588) converted (in-tree), conversion of out-of-tree components possible, whether they use pflag ([external-provisioner](https://github.com/kubernetes-csi/external-provisioner/pull/639)] or plain Go flags ([node-driver-registrar](https://github.com/kubernetes-csi/node-driver-registrar/pull/259)) |
874 | 916 | - Gathered feedback from developers and surveys
|
875 | 917 | - New APIs in `k8s.io/klog/v2` no longer marked as experimental
|
876 | 918 |
|
@@ -1037,6 +1079,11 @@ Revert commits that changed log calls.
|
1037 | 1079 |
|
1038 | 1080 | ## Implementation History
|
1039 | 1081 |
|
| 1082 | +* Kubernetes 1.24: initial alpha |
| 1083 | +* Kubernetes 1.27: parts of kube-controller-manager converted |
| 1084 | +* Kubernetes 1.28: kube-controller-manager converted completely, relationship |
| 1085 | + with log/slog in Go 1.21 clarified |
| 1086 | + |
1040 | 1087 | ## Drawbacks
|
1041 | 1088 |
|
1042 | 1089 | Supporting contextual logging is a key design decision that has implications
|
@@ -1098,3 +1145,10 @@ would have removed all legacy code from Kubernetes. However, that transition
|
1098 | 1145 | would have been complicated and forced all consumers of Kubernetes code to
|
1099 | 1146 | adjust their code. Therefore the scope of the KEP was reduced from "remove
|
1100 | 1147 | dependency on klog" to "remove dependency on global logger in klog".
|
| 1148 | + |
| 1149 | +### Use log/slog instead of klog+logr |
| 1150 | + |
| 1151 | +This isn't viable because `slog` doesn't provide a mechanism to pass a logger |
| 1152 | +through a context. Therefore it would not be possible to support contextual |
| 1153 | +logging in packages like client-go where adding an explicit logger parameter |
| 1154 | +would be a major API break. |
0 commit comments