diff --git a/log/logger.go b/log/logger.go index 859212db27a7..d41c0995e9f5 100644 --- a/log/logger.go +++ b/log/logger.go @@ -74,6 +74,20 @@ func NewLogger(dst io.Writer, options ...Option) Logger { for _, opt := range options { opt(&logCfg) } + if logCfg.JSONMarshal != nil { + 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 { diff --git a/log/options.go b/log/options.go index 22a1eb22841f..30897eb4af6c 100644 --- a/log/options.go +++ b/log/options.go @@ -1,6 +1,7 @@ package log import ( + "encoding/json" "time" "github.com/rs/zerolog" @@ -8,22 +9,24 @@ import ( // 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) } type Option func(*Config) @@ -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 { + return func(cfg *Config) { + cfg.JSONMarshal = f + } +}