@@ -30,6 +30,12 @@ import (
3030 "go.uber.org/zap/zapcore"
3131)
3232
33+ // EncoderConfigOption is a function that can modify a `zapcore.EncoderConfig`.
34+ type EncoderConfigOption func (* zapcore.EncoderConfig )
35+
36+ // NewEncoderFunc is a function that creates an Encoder using the provided EncoderConfigOptions.
37+ type NewEncoderFunc func (... EncoderConfigOption ) zapcore.Encoder
38+
3339// New returns a brand new Logger configured with Opts. It
3440// uses KubeAwareEncoder which adds Type information and
3541// Namespace/Name to the log.
@@ -65,6 +71,22 @@ func Encoder(encoder zapcore.Encoder) func(o *Options) {
6571 }
6672}
6773
74+ func newJSONEncoder (opts ... EncoderConfigOption ) zapcore.Encoder {
75+ encoderConfig := zap .NewProductionEncoderConfig ()
76+ for _ , opt := range opts {
77+ opt (& encoderConfig )
78+ }
79+ return zapcore .NewJSONEncoder (encoderConfig )
80+ }
81+
82+ func newConsoleEncoder (opts ... EncoderConfigOption ) zapcore.Encoder {
83+ encoderConfig := zap .NewDevelopmentEncoderConfig ()
84+ for _ , opt := range opts {
85+ opt (& encoderConfig )
86+ }
87+ return zapcore .NewConsoleEncoder (encoderConfig )
88+ }
89+
6890// Level sets the the minimum enabled logging level e.g Debug, Info
6991// See Options.Level
7092func Level (level zapcore.LevelEnabler ) func (o * Options ) {
@@ -99,6 +121,14 @@ type Options struct {
99121 // Encoder configures how Zap will encode the output. Defaults to
100122 // console when Development is true and JSON otherwise
101123 Encoder zapcore.Encoder
124+ // EncoderConfigOptions can modify the EncoderConfig needed to initialize an Encoder.
125+ // See https://godoc.org/go.uber.org/zap/zapcore#EncoderConfig for the list of options
126+ // that can be configured.
127+ // Note that the EncoderConfigOptions are not applied when the Encoder option is already set.
128+ EncoderConfigOptions []EncoderConfigOption
129+ // NewEncoder configures Encoder using the provided EncoderConfigOptions.
130+ // Note that the NewEncoder function is not used when the Encoder option is already set.
131+ NewEncoder NewEncoderFunc
102132 // DestWritter controls the destination of the log output. Defaults to
103133 // os.Stderr.
104134 DestWritter io.Writer
@@ -121,9 +151,8 @@ func (o *Options) addDefaults() {
121151 }
122152
123153 if o .Development {
124- if o .Encoder == nil {
125- encCfg := zap .NewDevelopmentEncoderConfig ()
126- o .Encoder = zapcore .NewConsoleEncoder (encCfg )
154+ if o .NewEncoder == nil {
155+ o .NewEncoder = newConsoleEncoder
127156 }
128157 if o .Level == nil {
129158 lvl := zap .NewAtomicLevelAt (zap .DebugLevel )
@@ -136,9 +165,8 @@ func (o *Options) addDefaults() {
136165 o .ZapOpts = append (o .ZapOpts , zap .Development ())
137166
138167 } else {
139- if o .Encoder == nil {
140- encCfg := zap .NewProductionEncoderConfig ()
141- o .Encoder = zapcore .NewJSONEncoder (encCfg )
168+ if o .NewEncoder == nil {
169+ o .NewEncoder = newJSONEncoder
142170 }
143171 if o .Level == nil {
144172 lvl := zap .NewAtomicLevelAt (zap .InfoLevel )
@@ -157,6 +185,9 @@ func (o *Options) addDefaults() {
157185 }))
158186 }
159187 }
188+ if o .Encoder == nil {
189+ o .Encoder = o .NewEncoder (o .EncoderConfigOptions ... )
190+ }
160191 o .ZapOpts = append (o .ZapOpts , zap .AddStacktrace (o .StacktraceLevel ))
161192}
162193
@@ -182,7 +213,7 @@ func NewRaw(opts ...Opts) *zap.Logger {
182213// BindFlags will parse the given flagset for zap option flags and set the log options accordingly
183214// zap-devel: Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn)
184215// Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)
185- // zap-encoder: Zap log encoding ('json' or 'console')
216+ // zap-encoder: Zap log encoding (one of 'json' or 'console')
186217// zap-log-level: Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error',
187218// or any integer value > 0 which corresponds to custom debug levels of increasing verbosity")
188219// zap-stacktrace-level: Zap Level at and above which stacktraces are captured (one of 'info' or 'error')
@@ -195,10 +226,10 @@ func (o *Options) BindFlags(fs *flag.FlagSet) {
195226
196227 // Set Encoder value
197228 var encVal encoderFlag
198- encVal .setFunc = func (fromFlag zapcore. Encoder ) {
199- o .Encoder = fromFlag
229+ encVal .setFunc = func (fromFlag NewEncoderFunc ) {
230+ o .NewEncoder = fromFlag
200231 }
201- fs .Var (& encVal , "zap-encoder" , "Zap log encoding ('json' or 'console')" )
232+ fs .Var (& encVal , "zap-encoder" , "Zap log encoding (one of 'json' or 'console')" )
202233
203234 // Set the Log Level
204235 var levelVal levelFlag
@@ -221,10 +252,10 @@ func (o *Options) BindFlags(fs *flag.FlagSet) {
221252// UseFlagOptions configures the logger to use the Options set by parsing zap option flags from the CLI.
222253// opts := zap.Options{}
223254// opts.BindFlags(flag.CommandLine)
255+ // flag.Parse()
224256// log := zap.New(zap.UseFlagOptions(&opts))
225257func UseFlagOptions (in * Options ) Opts {
226258 return func (o * Options ) {
227259 * o = * in
228- o .addDefaults ()
229260 }
230261}
0 commit comments