feat(log): support customization of log json marshal (#18429)
Co-authored-by: Julien Robert <julien@rbrt.fr> Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
parent
393eaef817
commit
b494d6a23e
@ -22,6 +22,8 @@ Each entry must include the Github issue reference in the following format:
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
* [#18429](https://github.com/cosmos/cosmos-sdk/pull/18429) Support customization of log json marshal.
|
||||
|
||||
## [v1.2.1](https://github.com/cosmos/cosmos-sdk/releases/tag/log/v1.2.1) - 2023-08-25
|
||||
|
||||
* [#17532](https://github.com/cosmos/cosmos-sdk/pull/17532) Proper marshalling of `fmt.Stringer` (follow-up of [#17205](https://github.com/cosmos/cosmos-sdk/pull/17205)).
|
||||
|
||||
@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
zerolog.InterfaceMarshalFunc = func(i interface{}) ([]byte, error) {
|
||||
zerolog.InterfaceMarshalFunc = func(i any) ([]byte, error) {
|
||||
switch v := i.(type) {
|
||||
case json.Marshaler:
|
||||
return json.Marshal(i)
|
||||
@ -57,6 +57,22 @@ type Logger interface {
|
||||
Impl() any
|
||||
}
|
||||
|
||||
// WithJSONMarshal configures zerolog global json encoding.
|
||||
func WithJSONMarshal(marshaler func(v any) ([]byte, error)) {
|
||||
zerolog.InterfaceMarshalFunc = func(i any) ([]byte, error) {
|
||||
switch v := i.(type) {
|
||||
case json.Marshaler:
|
||||
return marshaler(i)
|
||||
case encoding.TextMarshaler:
|
||||
return marshaler(i)
|
||||
case fmt.Stringer:
|
||||
return marshaler(v.String())
|
||||
default:
|
||||
return marshaler(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type zeroLogWrapper struct {
|
||||
*zerolog.Logger
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user