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)) +}