From 9175c47d173882b5dc4c307b582b415692a1f690 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Wed, 16 Feb 2022 12:17:47 +0100 Subject: [PATCH] enhance and fix log calls Some of these changes are cosmetic (repeatedly calling klog.V instead of reusing the result), others address real issues: - Logging a message only above a certain verbosity threshold without recording that verbosity level (if klog.V().Enabled() { klog.Info... }): this matters when using a logging backend which records the verbosity level. - Passing a format string with parameters to a logging function that doesn't do string formatting. All of these locations where found by the enhanced logcheck tool from https://github.com/kubernetes/klog/pull/297. In some cases it reports false positives, but those can be suppressed with source code comments. Kubernetes-commit: edffc700a43e610f641907290a5152ca593bad79 --- rest/request.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rest/request.go b/rest/request.go index 86f51229e1..ef94353011 100644 --- a/rest/request.go +++ b/rest/request.go @@ -1051,13 +1051,13 @@ func truncateBody(body string) string { // allocating a new string for the body output unless necessary. Uses a simple heuristic to determine // whether the body is printable. func glogBody(prefix string, body []byte) { - if klog.V(8).Enabled() { + if klogV := klog.V(8); klogV.Enabled() { if bytes.IndexFunc(body, func(r rune) bool { return r < 0x0a }) != -1 { - klog.Infof("%s:\n%s", prefix, truncateBody(hex.Dump(body))) + klogV.Infof("%s:\n%s", prefix, truncateBody(hex.Dump(body))) } else { - klog.Infof("%s: %s", prefix, truncateBody(string(body))) + klogV.Infof("%s: %s", prefix, truncateBody(string(body))) } } }