Merge pull request #109194 from pohly/log-tests

log tests
This commit is contained in:
Kubernetes Prow Robot
2022-06-10 03:07:46 -07:00
committed by GitHub
11 changed files with 1421 additions and 2 deletions

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

@@ -28,6 +28,7 @@ import (
"github.com/stretchr/testify/assert"
"go.uber.org/zap/zapcore"
"k8s.io/component-base/config"
"k8s.io/klog/v2"
)
@@ -239,7 +240,9 @@ func TestKlogIntegration(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
var buffer bytes.Buffer
writer := zapcore.AddSync(&buffer)
logger, _ := NewJSONLogger(100, writer, nil, nil)
// This level is high enough to enable all log messages from this test.
verbosityLevel := config.VerbosityLevel(100)
logger, _ := NewJSONLogger(verbosityLevel, writer, nil, nil)
klog.SetLogger(logger)
defer klog.ClearLogger()

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")