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

feat(log): support customization of log json marshal #18429

Merged
merged 13 commits into from
Nov 12, 2023
1 change: 1 addition & 0 deletions log/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Each entry must include the Github issue reference in the following format:
# Changelog

## [Unreleased]
* [#18429](https://github.com/cosmos/cosmos-sdk/pull/18429) support customization of log json marshal.
lilasxie marked this conversation as resolved.
Show resolved Hide resolved

## [v1.2.1](https://github.com/cosmos/cosmos-sdk/releases/tag/log/v1.2.1) - 2023-08-25

Expand Down
14 changes: 14 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ func NewLogger(dst io.Writer, options ...Option) Logger {
for _, opt := range options {
opt(&logCfg)
}
if logCfg.JSONMarshal != nil {
lilasxie marked this conversation as resolved.
Show resolved Hide resolved
zerolog.InterfaceMarshalFunc = func(i interface{}) ([]byte, error) {
switch v := i.(type) {
case json.Marshaler:
return logCfg.JSONMarshal(i)
case encoding.TextMarshaler:
return logCfg.JSONMarshal(i)
case fmt.Stringer:
return logCfg.JSONMarshal(v.String())
default:
return logCfg.JSONMarshal(i)
}
}
}

output := dst
if !logCfg.OutputJSON {
Expand Down
34 changes: 22 additions & 12 deletions log/options.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
package log

import (
"encoding/json"
"time"

"github.com/rs/zerolog"
)

// defaultConfig has all the options disabled, except Color and TimeFormat
var defaultConfig = Config{
Level: zerolog.NoLevel,
Filter: nil,
OutputJSON: false,
Color: true,
StackTrace: false,
TimeFormat: time.Kitchen,
Level: zerolog.NoLevel,
Filter: nil,
OutputJSON: false,
Color: true,
StackTrace: false,
TimeFormat: time.Kitchen,
JSONMarshal: json.Marshal,
}

// Config defines configuration for the logger.
type Config struct {
Level zerolog.Level
Filter FilterFunc
OutputJSON bool
Color bool
StackTrace bool
TimeFormat string
Level zerolog.Level
Filter FilterFunc
OutputJSON bool
Color bool
StackTrace bool
TimeFormat string
JSONMarshal func(v interface{}) ([]byte, error)
lilasxie marked this conversation as resolved.
Show resolved Hide resolved
}

type Option func(*Config)
Expand Down Expand Up @@ -87,3 +90,10 @@ func TraceOption(val bool) Option {
cfg.StackTrace = val
}
}

// JSONMarshalOption add option to configure custom JSON encoding
func JSONMarshalOption(f func(v interface{}) ([]byte, error)) Option {
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
lilasxie marked this conversation as resolved.
Show resolved Hide resolved
return func(cfg *Config) {
cfg.JSONMarshal = f
}
}
Loading