Skip to content

How to test an app that logs zap.Strings fields #1210

Answered by abhinav
slashformotion asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @slashformotion,

You can use LoggedEntry.ContextMap to get a map[string]interface{} to compare against. This will contain only primitive values:

assert.ElementsMatch(t, []interface{}{"scope1", "scope2"}, log.ContextMap()["scopes"])

Asserting on the Field directly means you're dealing with Zap's internal representation.

Using Field directly

You can still operate directly on it if you prefer, you just have to interpret it per the contract of Field.
In this case, for example: if the type is ArrayMarshalerType,
the only thing that's part of the public contract is that Interface holds a zapcore.ArrayMarshaler.
So you have to interpret that with a custom ArrayEncoder, or with the MapObjectE…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by sywhang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants