logcheck: enable helper check

This is in preparation of converting code to contextual logging.
This commit is contained in:
Patrick Ohly 2022-04-05 20:33:07 +02:00
parent 7b8d711d02
commit 991d848e34
4 changed files with 15 additions and 1 deletions

View File

@ -20,3 +20,14 @@ structured k8s.io/kubernetes/cmd/kubelet/.*
structured k8s.io/kubernetes/pkg/kubelet/.*
structured k8s.io/kubernetes/pkg/proxy/.*
structured k8s.io/kubernetes/pkg/scheduler/.*
# The following packages have been migrated to contextual logging.
# Packages matched here do not have to be listed above because
# "contextual" implies "structured".
# TODO next: contextual k8s.io/kubernetes/pkg/scheduler/.*
# As long as contextual logging is alpha or beta, all WithName, WithValues,
# NewContext calls have to go through klog. Once it is GA, we can lift
# this restriction. Whether we then do a global search/replace remains
# to be decided.
with-helpers .*

View File

@ -58,7 +58,7 @@ func NewLoggerCommand() *cobra.Command {
}
// Initialize contextual logging.
logger := klog.Background().WithName("example").WithValues("foo", "bar")
logger := klog.LoggerWithValues(klog.LoggerWithName(klog.Background(), "example"), "foo", "bar")
ctx := klog.NewContext(context.Background(), logger)
runLogger(ctx)

View File

@ -78,6 +78,7 @@ func TestZapLoggerInfo(t *testing.T) {
writer := zapcore.AddSync(&buffer)
sampleInfoLogger, _ := NewJSONLogger(0, writer, nil, nil)
for _, name := range data.names {
// nolint:logcheck // This intentionally ignore the feature gate and always tests with a name.
sampleInfoLogger = sampleInfoLogger.WithName(name)
}
// nolint:logcheck // The linter cannot and doesn't need to check the key/value pairs.

View File

@ -128,7 +128,9 @@ func testContextualLogging(t *testing.T, enabled bool) {
defer klog.EnableContextualLogging(true)
ctx := context.Background()
// nolint:logcheck // This intentionally adds a name independently of the feature gate.
logger := klog.NewKlogr().WithName("contextual")
// nolint:logcheck // This intentionally creates a new context independently of the feature gate.
ctx = logr.NewContext(ctx, logger)
if enabled {
assert.Equal(t, logger, klog.FromContext(ctx), "FromContext")