Skip to content

Commit

Permalink
Backport previous NewTestLogger flags
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanos committed Mar 6, 2025
1 parent d41f6ed commit 1269009
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
11 changes: 6 additions & 5 deletions common/log/zap_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ import (
const (
skipForZapLogger = 3
// we put a default message when it is empty so that the log can be searchable/filterable
defaultMsgForEmpty = "none"
testLogFormatEnvVar = "TEMPORAL_TEST_LOG_FORMAT" // set to "json" for json logs in tests
testLogLevelEnvVar = "TEMPORAL_TEST_LOG_LEVEL" // set to "debug" for debug level logs in tests
defaultMsgForEmpty = "none"
// TODO: once `NewTestLogger` has been removed, move these vars into testlogger.TestLogger
TestLogFormatEnvVar = "TEMPORAL_TEST_LOG_FORMAT" // set to "json" for json logs in tests
TestLogLevelEnvVar = "TEMPORAL_TEST_LOG_LEVEL" // set to "debug" for debug level logs in tests
)

type (
Expand All @@ -55,13 +56,13 @@ var _ Logger = (*zapLogger)(nil)

// NewTestLogger returns a logger for tests
func NewTestLogger() *zapLogger {
format := os.Getenv(testLogFormatEnvVar)
format := os.Getenv(TestLogFormatEnvVar)
if format == "" {
format = "console"
}

logger := BuildZapLogger(Config{
Level: os.Getenv(testLogLevelEnvVar),
Level: os.Getenv(TestLogLevelEnvVar),
Format: format,
Development: true,
})
Expand Down
33 changes: 11 additions & 22 deletions common/testing/testlogger/testlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package testlogger

import (
"cmp"
"container/list"
"fmt"
"os"
Expand Down Expand Up @@ -156,7 +157,6 @@ type sharedTestLoggerState struct {
}
mode Mode
logExpectations bool
logCaller bool
level zapcore.Level
}

Expand Down Expand Up @@ -204,18 +204,6 @@ func LogLevel(level zapcore.Level) LoggerOption {
}
}

func WithoutCaller() LoggerOption {
return func(t *TestLogger) {
t.state.logCaller = false
}
}

func LogCaller() LoggerOption {
return func(t *TestLogger) {
t.state.logCaller = false
}
}

// SetLogLevel overrides the temporal test log level during this test.
func SetLogLevel(tt CleanupCapableT, level zapcore.Level) LoggerOption {
return func(t *TestLogger) {
Expand All @@ -239,7 +227,6 @@ func NewTestLogger(t TestingT, mode Mode, opts ...LoggerOption) *TestLogger {
t: t,
logExpectations: false,
level: zapcore.DebugLevel,
logCaller: true,
mode: mode,
},
}
Expand All @@ -255,14 +242,16 @@ func NewTestLogger(t TestingT, mode Mode, opts ...LoggerOption) *TestLogger {
opt(tl)
}
if tl.wrapped == nil {
ztl := zaptest.NewLogger(t,
zaptest.Level(tl.state.level),
zaptest.WrapOptions(
zap.AddStacktrace(zap.ErrorLevel),
zap.WithCaller(tl.state.logCaller),
),
)
tl.wrapped = log.NewZapLogger(ztl).Skip(1)
tl.wrapped = log.NewZapLogger(
log.BuildZapLogger(
log.Config{
Level: cmp.Or(os.Getenv(log.TestLogLevelEnvVar), tl.state.level.String()),
Format: cmp.Or(os.Getenv(log.TestLogFormatEnvVar), "console"),
}).
WithOptions(
zap.AddStacktrace(zap.ErrorLevel), // only include stack traces for logs with level error and above
)).
Skip(1)
}

// Only possible with a *testing.T until *rapid.T supports `Cleanup`
Expand Down

0 comments on commit 1269009

Please sign in to comment.