Skip to content

Commit

Permalink
✨ Zap: Add JSONEncoder and ConsoleEncoder opts
Browse files Browse the repository at this point in the history
Currently, setting the encoding is pretty annoying and requires to
import two more packages apart from log/zap:

```
encoderConfig := uberzap.NewProductionEncoderConfig()
zap.New(zap.Encoder(zapcore.NewJSONEncoder(encoderConfig))
```

This PR adds a JSONEncoder and ConsoleEncoder opt to simplify this
  • Loading branch information
alvaroaleman committed Oct 2, 2020
1 parent af2f5b1 commit 5bdbd17
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/log/zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ func Encoder(encoder zapcore.Encoder) func(o *Options) {
}
}

// JSONEncoder configures the logger to use a JSON Encoder
func JSONEncoder(opts ...EncoderConfigOption) func(o *Options) {
return func(o *Options) {
o.Encoder = newJSONEncoder(opts...)
}
}

func newJSONEncoder(opts ...EncoderConfigOption) zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
for _, opt := range opts {
Expand All @@ -79,6 +86,13 @@ func newJSONEncoder(opts ...EncoderConfigOption) zapcore.Encoder {
return zapcore.NewJSONEncoder(encoderConfig)
}

// ConsoleEncoder configures the logger to use a Console encoder
func ConsoleEncoder(opts ...EncoderConfigOption) func(o *Options) {
return func(o *Options) {
o.Encoder = newConsoleEncoder(opts...)
}
}

func newConsoleEncoder(opts ...EncoderConfigOption) zapcore.Encoder {
encoderConfig := zap.NewDevelopmentEncoderConfig()
for _, opt := range opts {
Expand Down

0 comments on commit 5bdbd17

Please sign in to comment.