Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port previous NewTestLogger flags over #7432

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 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 @@ -54,14 +55,15 @@ type (
var _ Logger = (*zapLogger)(nil)

// NewTestLogger returns a logger for tests
// Deprecated: Use testlogger.TestLogger instead.
func NewTestLogger() *zapLogger {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a ticket to remove this function and replace it with testlogger.TestLogger.

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we port this too? it looks like it controls whether it panics on dpanic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new TestLogger already has a flag panicOnDPanic which by default is true.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need envs to control test logger? Do we use it anywhere?

Copy link
Contributor Author

@stephanos stephanos Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

David does to switch it to JSON format; and I noticed that I actually set up my IDE to log on error level instead of debug level. So that's two use cases already.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know we have it.

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

Comment on lines -207 to -218
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work together with log.BuildZapLogger which defaults to disabling this. Removing it for now.

// 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
Loading