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

Feature: customizd interface marshaler #317

Closed
povsister opened this issue May 15, 2021 · 2 comments · Fixed by #318
Closed

Feature: customizd interface marshaler #317

povsister opened this issue May 15, 2021 · 2 comments · Fixed by #318

Comments

@povsister
Copy link
Contributor

Currently, when using logger.Debug().Interface("request_body", reqBody).Msg("request sent") to log the request detail with customized types, zerolog first assert if the "reqBody" is a LogObjectMarshaler or not, if not, it tries the std json marshaler to serialize the request body.

As you can see in the code snippet. It's hard coded to json.Marshal.

// AppendInterface marshals the input interface to a string and
// appends the encoded string to the input byte slice.
func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte {
marshaled, err := json.Marshal(i)
if err != nil {
return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err))
}
return append(dst, marshaled...)
}

However, for performance and convenient, mostly we do not use the standard json package for json encode/decode now.
I hope zerolog could have a package level InterfaceMarshaler variable, and use it to marshal object when needed.
It has the same signature with json.Marshal, so everyone can replace the internal reflection based marshaler at runtime without forking zerolog.

type InterfaceMarshaler func(obj interface{}) (data []byte, err error)

PS: I am using jsoniter and have registered many customized encoders/decoders for customized types. That's why I hope zerolog can benefit from jsoniter without additional works.

@rs
Copy link
Owner

rs commented May 16, 2021

Can you please send a PR?

@povsister
Copy link
Contributor Author

Can you please send a PR?

Sure. Please take a look. #318

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants