fix(log): add fallback to Stringer when type do not implement json.Marshaler interface (#17205)

This commit is contained in:
zakir 2023-07-31 18:33:15 +08:00 committed by GitHub
parent 60ead8dd24
commit 0b7d2d310b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -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).

View File

@ -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"