From a38646840657ffa3023f3cbf43786a4ed4b9c3e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Fri, 20 Sep 2024 15:28:35 +0200 Subject: [PATCH] Allow to change the default log level --- pkg/output/logging/context.go | 2 +- pkg/output/logging/log_level.go | 43 ++++++++++++++++++++++++++++ pkg/output/logging/log_level_test.go | 36 +++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 pkg/output/logging/log_level.go create mode 100644 pkg/output/logging/log_level_test.go diff --git a/pkg/output/logging/context.go b/pkg/output/logging/context.go index c726db6700..fac53a0a0c 100644 --- a/pkg/output/logging/context.go +++ b/pkg/output/logging/context.go @@ -126,7 +126,7 @@ func createDefaultLogger(ctx context.Context) *zap.Logger { ec.EncodeTime = ElapsedMillisTimeEncoder(time.Now()) ec.ConsoleSeparator = " " - lvl := activeLogLevel(zapcore.WarnLevel) + lvl := activeLogLevel(LogLevelFromContext(ctx)) logger := zap.New(zapcore.NewCore( zapcore.NewConsoleEncoder(ec), zapcore.AddSync(errout), diff --git a/pkg/output/logging/log_level.go b/pkg/output/logging/log_level.go new file mode 100644 index 0000000000..72db8ee75f --- /dev/null +++ b/pkg/output/logging/log_level.go @@ -0,0 +1,43 @@ +/* + Copyright 2024 The Knative Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package logging + +import ( + "context" + + "go.uber.org/zap/zapcore" + pkgcontext "knative.dev/client/pkg/context" +) + +var logLevelKey = struct{}{} + +// LogLevelFromContext returns the default log level for the logger. +func LogLevelFromContext(ctx context.Context) zapcore.Level { + if val, ok := ctx.Value(logLevelKey).(zapcore.Level); ok { + return val + } else { + if pkgcontext.TestingTFromContext(ctx) != nil { + return zapcore.DebugLevel + } + return zapcore.WarnLevel + } +} + +// WithLogLevel will set given log level as the default one in given context. +func WithLogLevel(ctx context.Context, level zapcore.Level) context.Context { + return context.WithValue(ctx, logLevelKey, level) +} diff --git a/pkg/output/logging/log_level_test.go b/pkg/output/logging/log_level_test.go new file mode 100644 index 0000000000..7c7595e91e --- /dev/null +++ b/pkg/output/logging/log_level_test.go @@ -0,0 +1,36 @@ +/* + Copyright 2024 The Knative Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package logging_test + +import ( + "context" + "testing" + + "go.uber.org/zap/zapcore" + "gotest.tools/v3/assert" + pkgcontext "knative.dev/client/pkg/context" + "knative.dev/client/pkg/output/logging" +) + +func TestLogLevel(t *testing.T) { + ctx := context.TODO() + assert.Equal(t, zapcore.WarnLevel, logging.LogLevelFromContext(ctx)) + ctx = pkgcontext.WithTestingT(ctx, t) + assert.Equal(t, zapcore.DebugLevel, logging.LogLevelFromContext(ctx)) + ctx = logging.WithLogLevel(ctx, zapcore.InfoLevel) + assert.Equal(t, zapcore.InfoLevel, logging.LogLevelFromContext(ctx)) +}