Skip to content

Commit

Permalink
fix(log): add fallback to Stringer when type do not implement `json…
Browse files Browse the repository at this point in the history
….Marshaler` interface (#17205)
  • Loading branch information
zakir-code authored Jul 31, 2023
1 parent 60ead8d commit 0b7d2d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions log/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
18 changes: 18 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package log

import (
"encoding"
"encoding/json"
"fmt"
"io"

"github.com/pkg/errors"
"github.com/rs/zerolog"
"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"

Expand Down

0 comments on commit 0b7d2d3

Please sign in to comment.