fix(log): add fallback to Stringer when type do not implement json.Marshaler interface (#17205)
This commit is contained in:
parent
60ead8dd24
commit
0b7d2d310b
@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
||||
|
||||
* [#17194](https://github.com/cosmos/cosmos-sdk/pull/17194) Avoid repeating parse log level in filterFunc.
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* [#17205](https://github.com/cosmos/cosmos-sdk/pull/17205) Fix types that do not implement the `json.Marshaler` interface.
|
||||
|
||||
## [v1.1.0](https://github.com/cosmos/cosmos-sdk/releases/tag/log/v1.1.0) - 2023-04-27
|
||||
|
||||
* [#15956](https://github.com/cosmos/cosmos-sdk/pull/15956) Introduce options to configure logger (enable/disable colored output, customize log timestamps).
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@ -8,6 +11,21 @@ import (
|
||||
"github.com/rs/zerolog/pkgerrors"
|
||||
)
|
||||
|
||||
func init() {
|
||||
zerolog.InterfaceMarshalFunc = func(i interface{}) ([]byte, error) {
|
||||
switch v := i.(type) {
|
||||
case json.Marshaler:
|
||||
return json.Marshal(i)
|
||||
case encoding.TextMarshaler:
|
||||
return json.Marshal(i)
|
||||
case fmt.Stringer:
|
||||
return fmt.Appendf([]byte("\""), "%s%s", v.String(), "\""), nil
|
||||
default:
|
||||
return json.Marshal(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ModuleKey defines a module logging key.
|
||||
const ModuleKey = "module"
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user