Skip to content

Commit

Permalink
Addressed PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bharathi-tenneti committed Jul 27, 2020
1 parent eccba7c commit c480736
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
19 changes: 9 additions & 10 deletions pkg/log/zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ import (
"go.uber.org/zap/zapcore"
)

// EncoderConfigOption is being used to pass options
// for configuring console/json encoder.
// EncoderConfigOption is a function that can modify a `zapcore.EncoderConfig`.
type EncoderConfigOption func(*zapcore.EncoderConfig)

// NewEncoderFunc is a function that creates an Encoder using the provided EncoderConfigOptions.
Expand Down Expand Up @@ -105,18 +104,18 @@ func Encoder(encoder zapcore.Encoder) func(o *Options) {
}
}

func newJSONEncoder(ecfs ...EncoderConfigOption) zapcore.Encoder {
func newJSONEncoder(opts ...EncoderConfigOption) zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
for _, f := range ecfs {
f(&encoderConfig)
for _, opt := range opts {
opt(&encoderConfig)
}
return zapcore.NewJSONEncoder(encoderConfig)
}

func newConsoleEncoder(ecfs ...EncoderConfigOption) zapcore.Encoder {
func newConsoleEncoder(opts ...EncoderConfigOption) zapcore.Encoder {
encoderConfig := zap.NewDevelopmentEncoderConfig()
for _, f := range ecfs {
f(&encoderConfig)
for _, opt := range opts {
opt(&encoderConfig)
}
return zapcore.NewConsoleEncoder(encoderConfig)
}
Expand Down Expand Up @@ -247,7 +246,7 @@ func NewRaw(opts ...Opts) *zap.Logger {
// BindFlags will parse the given flagset for zap option flags and set the log options accordingly
// zap-devel: Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn)
// Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)
// zap-encoder: Zap log encoding (Eg., 'json' or 'console')
// zap-encoder: Zap log encoding (one of 'json' or 'console')
// zap-log-level: Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error',
// or any integer value > 0 which corresponds to custom debug levels of increasing verbosity")
// zap-stacktrace-level: Zap Level at and above which stacktraces are captured (one of 'info' or 'error')
Expand All @@ -263,7 +262,7 @@ func (o *Options) BindFlags(fs *flag.FlagSet) {
encVal.setFunc = func(fromFlag NewEncoderFunc) {
o.NewEncoder = fromFlag
}
fs.Var(&encVal, "zap-encoder", "Zap log encoding (Eg., 'json' or 'console')")
fs.Var(&encVal, "zap-encoder", "Zap log encoding (one of 'json' or 'console')")

// Set the Log Level
var levelVal levelFlag
Expand Down
13 changes: 8 additions & 5 deletions pkg/log/zap/zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@ var _ = Describe("Zap log level flag options setup", func() {
Expect(out.StacktraceLevel).To(BeNil())
Expect(out.EncoderConfigOptions).To(BeNil())
})
It("Should set dev=false with log options.", func() {
args := []string{"--zap-log-level=2"}
It("Should set dev=false", func() {
args := []string{"--zap-devel=false"}
fromFlags.BindFlags(&fs)
if err := fs.Parse(args); err != nil {
Expect(err).ToNot(HaveOccurred())
Expand All @@ -485,14 +485,17 @@ var _ = Describe("Zap log level flag options setup", func() {
UseFlagOptions(&fromFlags)(&out)

Expect(out.Development).To(BeFalse())
Expect(out.Level.Enabled(zapcore.ErrorLevel)).To(BeTrue())
Expect(out.Encoder).To(BeNil())
Expect(out.Level).To(BeNil())
Expect(out.StacktraceLevel).To(BeNil())
Expect(out.EncoderConfigOptions).To(BeNil())

})
})

Context("with encoder options provided programmatically.", func() {

It("Should set Console Encoder, with given Nanos TimEncoder option.", func() {
It("Should set Console Encoder, with given Nanos TimeEncoder option.", func() {
logOut := new(bytes.Buffer)
f := func(ec *zapcore.EncoderConfig) {
if err := ec.EncodeTime.UnmarshalText([]byte("nanos")); err != nil {
Expand All @@ -512,7 +515,7 @@ var _ = Describe("Zap log level flag options setup", func() {
Expect(string(outRaw)).ShouldNot(ContainSubstring("."))

})
It("Should set JSON Encoder, with given Millis TimEncoder option, and MessageKey", func() {
It("Should set JSON Encoder, with given Millis TimeEncoder option, and MessageKey", func() {
logOut := new(bytes.Buffer)
f := func(ec *zapcore.EncoderConfig) {
ec.MessageKey = "MillisTimeFormat"
Expand Down

0 comments on commit c480736

Please sign in to comment.