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/observer: Support filtering by message or field #386

Merged
merged 2 commits into from
Mar 25, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Handle duplicate fields
  • Loading branch information
prashantv committed Mar 24, 2017
commit cadf7cc380862bcfcf781e7497840688bbe46253
5 changes: 2 additions & 3 deletions zaptest/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ func (o *ObservedLogs) FilterMessage(msg string) *ObservedLogs {
func (o *ObservedLogs) FilterField(field zapcore.Field) *ObservedLogs {
return o.filter(func(e LoggedEntry) bool {
for _, ctxField := range e.Context {
if ctxField.Key != field.Key {
continue
if ctxField == field {
return true
}
return ctxField == field
}
return false
})
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 @@ -133,6 +133,10 @@ func TestFilters(t *testing.T) {
Entry: zapcore.Entry{Level: zap.InfoLevel, Message: "log b"},
Context: []zapcore.Field{zap.Int("a", 1), zap.Int("b", 2)},
},
{
Entry: zapcore.Entry{Level: zap.InfoLevel, Message: "log c"},
Context: []zapcore.Field{zap.Int("a", 1), zap.Namespace("ns"), zap.Int("a", 2)},
},
}

logger, sink := New(zap.InfoLevel)
Expand Down Expand Up @@ -160,6 +164,11 @@ func TestFilters(t *testing.T) {
filtered: sink.FilterMessage("log a").FilterField(zap.Int("b", 2)),
want: logs[1:2],
},
{
msg: "filter by field with duplicate fields",
filtered: sink.FilterField(zap.Int("a", 2)),
want: logs[3:4],
},
{
msg: "filter doesn't match any messages",
filtered: sink.FilterMessage("no match"),
Expand Down