Skip to content

Commit

Permalink
Allow to change the default log level
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Sep 20, 2024
1 parent d5d174c commit a386468
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/output/logging/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
43 changes: 43 additions & 0 deletions pkg/output/logging/log_level.go
Original file line number Diff line number Diff line change
@@ -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)
}
36 changes: 36 additions & 0 deletions pkg/output/logging/log_level_test.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit a386468

Please sign in to comment.