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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions zaptest/observer/observer.go
Expand Up @@ -80,6 +80,39 @@ func (o *ObservedLogs) AllUntimed() []LoggedEntry {
return ret
}

// FilterMessage filters entries to those that have the specified message.
func (o *ObservedLogs) FilterMessage(msg string) *ObservedLogs {
return o.filter(func(e LoggedEntry) bool {
return e.Message == msg
})
}

// FilterField filters entries to those that have the specified field.
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
}
return ctxField == field
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just use ctxField == field?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

continue vs return -- if we find a field with the right key, then the match is whether the field matches exactly. If the field mismatches on keys, we want to check other fields (since the filter only needs one field to match exactly).

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I should be a bit clearer. It's entirely possible to have multiple fields with the same key. Consider

logger.With(zap.Int("foo", 1), zap.Namespace("component"), zap.Int("foo", 2))

and a filter on zap.Int("foo", 2).

Given that, I don't think we want to explicitly compare keys; instead, I'd expect something more like

for _, f := range e.Context {
  if f == field {
    return true
  }
}
return false

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, added a test and supports multiple keys. As soon as any of the fields match with the specified key, the log entry will be returned.

}
return false
})
}

func (o *ObservedLogs) filter(match func(LoggedEntry) bool) *ObservedLogs {
o.mu.RLock()
defer o.mu.RUnlock()

var filtered []LoggedEntry
for _, entry := range o.logs {
if match(entry) {
filtered = append(filtered, entry)
}
}
return &ObservedLogs{logs: filtered}
}

func (o *ObservedLogs) add(log LoggedEntry) {
o.mu.Lock()
o.logs = append(o.logs, log)
Expand Down
54 changes: 54 additions & 0 deletions zaptest/observer/observer_test.go
Expand Up @@ -118,3 +118,57 @@ func TestObserverWith(t *testing.T) {
},
}, logs.All(), "expected no field sharing between With siblings")
}

func TestFilters(t *testing.T) {
logs := []LoggedEntry{
{
Entry: zapcore.Entry{Level: zap.InfoLevel, Message: "log a"},
Context: []zapcore.Field{zap.String("fStr", "1"), zap.Int("a", 1)},
},
{
Entry: zapcore.Entry{Level: zap.InfoLevel, Message: "log a"},
Context: []zapcore.Field{zap.String("fStr", "2"), zap.Int("b", 2)},
},
{
Entry: zapcore.Entry{Level: zap.InfoLevel, Message: "log b"},
Context: []zapcore.Field{zap.Int("a", 1), zap.Int("b", 2)},
},
}

logger, sink := New(zap.InfoLevel)
for _, log := range logs {
logger.Write(log.Entry, log.Context)
}

tests := []struct {
msg string
filtered *ObservedLogs
want []LoggedEntry
}{
{
msg: "filter by message",
filtered: sink.FilterMessage("log a"),
want: logs[0:2],
},
{
msg: "filter by field",
filtered: sink.FilterField(zap.String("fStr", "1")),
want: logs[0:1],
},
{
msg: "filter by message and field",
filtered: sink.FilterMessage("log a").FilterField(zap.Int("b", 2)),
want: logs[1:2],
},
{
msg: "filter doesn't match any messages",
filtered: sink.FilterMessage("no match"),
want: []LoggedEntry{},
},
}

for _, tt := range tests {
got := tt.filtered.AllUntimed()
assert.Equal(t, tt.want, got, tt.msg)
}
}