From 08ceb4d47192ba2810b4a385cb6c7a31c5866c10 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Mon, 5 Jan 2026 18:56:32 +0100 Subject: [PATCH] logging: warn about logger.V().Error This has gone wrong a few times in Kubernetes. In addition to the documentation update, a linter check is coming which flags these errors. --- .../migration-to-structured-logging.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/contributors/devel/sig-instrumentation/migration-to-structured-logging.md b/contributors/devel/sig-instrumentation/migration-to-structured-logging.md index 7bedf600b9a..ec02fad11bc 100644 --- a/contributors/devel/sig-instrumentation/migration-to-structured-logging.md +++ b/contributors/devel/sig-instrumentation/migration-to-structured-logging.md @@ -190,12 +190,16 @@ functions need to be used: - `klog.ErrorS` -> `logger.Error` - `klog.InfoS` -> `logger.Info` - `klog.V().InfoS` -> `logger.V().Info` +- `klog.V().ErrorS(err, ...)` -> `logger.V().Info(..., "err", err)` In all of these cases, `logger` is a `logr.Logger` instance. `klog.Logger` is -an alias for that type. Determining where that instance comes from is the main -challenge when migrating code to contextual logging. +an alias for that type. In contrast to klog, logr ignores verbosity for `Error` +calls and always emits the log. The semantic equivalent of `klog.V().ErrorS` is +therefore an `Info` call where the error is represented as a key/value pair. -Several new klog functions help with that: +Determining where the `Logger` instance comes from is the main challenge when +migrating code to contextual logging. Several new klog functions help with +that: - [`klog.FromContext`](https://pkg.go.dev/k8s.io/klog/v2#FromContext) - [`klog.Background`](https://pkg.go.dev/k8s.io/klog/v2#Background)