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

zaptest: add ability to filter observer logs by logger name #1452

Merged
merged 2 commits into from
Jul 22, 2024
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
7 changes: 7 additions & 0 deletions zaptest/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ func (o *ObservedLogs) FilterMessage(msg string) *ObservedLogs {
})
}

// FilterLoggerName filters entries to those logged through logger with the specified logger name.
func (o *ObservedLogs) FilterLoggerName(name string) *ObservedLogs {
return o.Filter(func(e LoggedEntry) bool {
return e.LoggerName == name
})
}

// FilterMessageSnippet filters entries to those that have a message containing the specified snippet.
func (o *ObservedLogs) FilterMessageSnippet(snippet string) *ObservedLogs {
return o.Filter(func(e LoggedEntry) bool {
Expand Down
9 changes: 9 additions & 0 deletions zaptest/observer/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ func TestFilters(t *testing.T) {
Entry: zapcore.Entry{Level: zap.ErrorLevel, Message: "warp core breach"},
Context: []zapcore.Field{zap.Int("b", 42)},
},
{
Entry: zapcore.Entry{Level: zap.ErrorLevel, Message: "msg", LoggerName: "my.logger"},
Context: []zapcore.Field{zap.Int("b", 42)},
},
}

logger, sink := New(zap.InfoLevel)
Expand Down Expand Up @@ -251,6 +255,11 @@ func TestFilters(t *testing.T) {
filtered: sink.FilterLevelExact(zap.WarnLevel),
want: logs[9:10],
},
{
msg: "filter logger name",
filtered: sink.FilterLoggerName("my.logger"),
want: logs[11:12],
},
}

for _, tt := range tests {
Expand Down