Skip to content

Commit

Permalink
refactor: refactor the implementation of json marshaler customization
Browse files Browse the repository at this point in the history
  • Loading branch information
lilasxie committed Nov 11, 2023
1 parent ee15a68 commit 757f332
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
39 changes: 23 additions & 16 deletions log/logger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package log

import (
"encoding"
"encoding/json"
"fmt"
"io"
Expand All @@ -11,6 +10,17 @@ import (
"github.com/rs/zerolog/pkgerrors"
)

func init() {
zerolog.InterfaceMarshalFunc = func(i interface{}) ([]byte, error) {
switch v := i.(type) {
case fmt.Stringer:
return json.Marshal(v.String())
default:
return json.Marshal(i)
}
}
}

// ModuleKey defines a module logging key.
const ModuleKey = "module"

Expand Down Expand Up @@ -42,6 +52,18 @@ type Logger interface {
Impl() any
}

// WithJSONMarshal configure zerolog global json encoding
func WithJSONMarshal(marshaler func(v any) ([]byte, error)) {
zerolog.InterfaceMarshalFunc = func(i interface{}) ([]byte, error) {
switch v := i.(type) {
case fmt.Stringer:
return marshaler(v.String())
default:
return marshaler(i)
}
}
}

type zeroLogWrapper struct {
*zerolog.Logger
}
Expand All @@ -59,21 +81,6 @@ func NewLogger(dst io.Writer, options ...Option) Logger {
for _, opt := range options {
opt(&logCfg)
}
// set default jsonMarshaler to json.Marshal for compatibility
jsonMarshaler := json.Marshal
if logCfg.JSONMarshal != nil {
jsonMarshaler = logCfg.JSONMarshal
}
zerolog.InterfaceMarshalFunc = func(i interface{}) ([]byte, error) {
switch v := i.(type) {
case json.Marshaler, encoding.TextMarshaler:
return jsonMarshaler(i)
case fmt.Stringer:
return jsonMarshaler(v.String())
default:
return jsonMarshaler(i)
}
}

output := dst
if !logCfg.OutputJSON {
Expand Down
34 changes: 12 additions & 22 deletions log/options.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
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,
JSONMarshal: json.Marshal,
Level: zerolog.NoLevel,
Filter: nil,
OutputJSON: false,
Color: true,
StackTrace: false,
TimeFormat: time.Kitchen,
}

// Config defines configuration for the logger.
type Config struct {
Level zerolog.Level
Filter FilterFunc
OutputJSON bool
Color bool
StackTrace bool
TimeFormat string
JSONMarshal func(v any) ([]byte, error)
Level zerolog.Level
Filter FilterFunc
OutputJSON bool
Color bool
StackTrace bool
TimeFormat string
}

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

// JSONMarshalOption add option to configure custom JSON encoding
func JSONMarshalOption(f func(v any) ([]byte, error)) Option {
return func(cfg *Config) {
cfg.JSONMarshal = f
}
}

0 comments on commit 757f332

Please sign in to comment.