Skip to content

Commit bbff785

Browse files
authored
Log the 'log.logger' key (#365)
1 parent ab36659 commit bbff785

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

logp/logptest/logger.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ type Logger struct {
6060
// replacement for a *logp.Logger, or the logger can be accessed via
6161
// [Logger.Logger]
6262
func NewFileLogger(t testing.TB, dir string) *Logger {
63-
encoderConfig := ecszap.ECSCompatibleEncoderConfig(zapcore.EncoderConfig{})
63+
encoderConfig := ecszap.ECSCompatibleEncoderConfig(zapcore.EncoderConfig{
64+
// As long as NameKey is NOT EMPTY it will be overridden
65+
// by the correct key. So we set it to 'enabled' to make it very
66+
// clear this is NOT the final key for the logger name.
67+
NameKey: "enabled",
68+
})
6469
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
6570
encoder := zapcore.NewJSONEncoder(encoderConfig)
6671

logp/logptest/logger_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
func TestNewFileLogger(t *testing.T) {
3535
logger := NewFileLogger(t, "")
36+
logger.Logger = logger.Named("test-logger")
3637
logger.Debug("foo")
3738

3839
assertLogFormat(t, logger.logFile.Name())
@@ -93,6 +94,7 @@ func assertLogFormat(t *testing.T, path string) {
9394
entry := struct {
9495
Timestamp string `json:"@timestamp"`
9596
LogLevel string `json:"log.level"`
97+
Logger string `json:"log.logger"`
9698
Message string `json:"message"`
9799
}{}
98100

@@ -112,6 +114,10 @@ func assertLogFormat(t *testing.T, path string) {
112114
if entry.Message == "" {
113115
t.Error("message cannot be empty")
114116
}
117+
118+
if entry.Logger == "" {
119+
t.Error("'log.logger' cannot be empty")
120+
}
115121
}
116122
}
117123

0 commit comments

Comments
 (0)