Skip to content

Creating custom object encoder with a primitive array member #1099

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

You must be logged in to vote

If you have a []string inside a ObjectMarshaler implementation,
you can re-use the zap.Strings field constructor
and add it to the encoder with Field.AddTo.

func (o Order) MarshalLogObject(enc zapcore.ObjectEncoder) error {
	enc.AddString("id", o.ID)
	enc.AddInt64("price", o.Price)
	zap.Strings("keywords", o.Keywords).AddTo(enc)
       // ...
}

Hope this helps!

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by abhinav
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
Converted from issue

This discussion was converted from issue #1073 on May 16, 2022 16:10.