Skip to content

zaptest/observer doesn't support filtering {map,slice,function} fields passed to zap.Any #443

Closed
@ZymoticB

Description

@ZymoticB
diff --git a/zaptest/observer/observer_test.go b/zaptest/observer/observer_test.go
index b66851e..a4738c0 100644
--- a/zaptest/observer/observer_test.go
+++ b/zaptest/observer/observer_test.go
@@ -141,6 +141,14 @@ func TestFilters(t *testing.T) {
                        Entry:   zapcore.Entry{Level: zap.InfoLevel, Message: "msg 1"},
                        Context: []zapcore.Field{zap.Int("a", 1), zap.Namespace("ns")},
                },
+               {
+                       Entry:   zapcore.Entry{Level: zap.InfoLevel, Message: "any map"},
+                       Context: []zapcore.Field{zap.Any("map", map[string]string{"a": "b"})},
+               },
+               {
+                       Entry:   zapcore.Entry{Level: zap.InfoLevel, Message: "any slice"},
+                       Context: []zapcore.Field{zap.Any("slice", []string{"a"})},
+               },
        }

        logger, sink := New(zap.InfoLevel)
@@ -188,6 +196,16 @@ func TestFilters(t *testing.T) {
                        filtered: sink.FilterMessageSnippet("a").FilterField(zap.Int("b", 2)),
                        want:     logs[1:2],
                },
+               {
+                       msg:      "filter for map",
+                       filtered: sink.FilterField(zap.Any("map2", map[string]string{"a": "b"})),
+                       want:     logs[4:5],
+               },
+               {
+                       msg:      "filter for slice",
+                       filtered: sink.FilterField(zap.Any("slice", []string{"a"})),
+                       want:     logs[5:6],
+               },
        }

        for _, tt := range tests {

This small patch adds two tests to the observer which will both panic as maps and slices are not comparable. This will also be true of functions but I don't think that's something valid to support.

The only way forward I'm aware of would be to type switch and special case these in FilterField. Is there any other option (reflect.DeepEquals I guess but that seems wasteful when the special cases are known and limited)? This result definitely surprised me as, as far as I know, these are valid fields to log.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions