Skip to content

Commit dd4c92b

Browse files
map added for levelStrings replacing switch, and formatting changes
1 parent 435f612 commit dd4c92b

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

pkg/log/zap/flags.go

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ import (
2828
"go.uber.org/zap/zapcore"
2929
)
3030

31+
var levelStrings = map[string]zapcore.Level{
32+
"debug": zap.DebugLevel,
33+
"-1": zap.DebugLevel,
34+
"info": zap.InfoLevel,
35+
"0": zap.InfoLevel,
36+
"error": zap.ErrorLevel,
37+
"2": zap.ErrorLevel,
38+
"dpanic": zap.DPanicLevel,
39+
"panic": zap.PanicLevel,
40+
"warn": zap.WarnLevel,
41+
"fatal": zap.FatalLevel,
42+
}
43+
3144
type encoderFlag struct {
3245
setFunc func(zapcore.Encoder)
3346
value string
@@ -75,32 +88,22 @@ type levelFlag struct {
7588
var _ pflag.Value = &levelFlag{}
7689

7790
func (ev *levelFlag) Set(flagValue string) error {
78-
lower := strings.ToLower(flagValue)
79-
var lvl int
80-
switch lower {
81-
case "debug", "-1":
82-
ev.setFunc(zap.NewAtomicLevelAt(zap.DebugLevel))
83-
case "info", "0":
84-
ev.setFunc(zap.NewAtomicLevelAt(zap.InfoLevel))
85-
case "error", "2":
86-
ev.setFunc(zap.NewAtomicLevelAt(zap.ErrorLevel))
87-
default:
88-
i, err := strconv.Atoi(lower)
91+
level, validLevel := levelStrings[strings.ToLower(flagValue)]
92+
if !validLevel {
93+
logLevel, err := strconv.Atoi(flagValue)
8994
if err != nil {
90-
return fmt.Errorf("invalid log level \"%s\"", flagValue)
95+
return fmt.Errorf("In here invalid log level \"%s\"", flagValue)
9196
}
92-
if i > 0 {
93-
fmt.Println("Iam here")
94-
lvl = -1 * i
95-
ev.setFunc(zap.NewAtomicLevelAt(zapcore.Level(int8(lvl))))
97+
if logLevel > 0 {
98+
intLevel := -1 * logLevel
99+
ev.setFunc(zap.NewAtomicLevelAt(zapcore.Level(int8(intLevel))))
96100
} else {
97-
return fmt.Errorf("invalid log level \"%s\"", flagValue)
101+
return fmt.Errorf("There invalid log level \"%s\"", flagValue)
98102
}
99103
}
100-
104+
ev.setFunc(zap.NewAtomicLevelAt(level))
101105
ev.value = flagValue
102106
return nil
103-
104107
}
105108

106109
func (ev *levelFlag) String() string {
@@ -119,25 +122,11 @@ type stackTraceFlag struct {
119122
var _ pflag.Value = &stackTraceFlag{}
120123

121124
func (ev *stackTraceFlag) Set(flagValue string) error {
122-
lower := strings.ToLower(flagValue)
123-
switch lower {
124-
case "debug":
125-
ev.setFunc(zap.NewAtomicLevelAt(zap.DebugLevel))
126-
case "info":
127-
ev.setFunc(zap.NewAtomicLevelAt(zap.InfoLevel))
128-
case "warn":
129-
ev.setFunc(zap.NewAtomicLevelAt(zap.WarnLevel))
130-
case "dpanic":
131-
ev.setFunc(zap.NewAtomicLevelAt(zap.DPanicLevel))
132-
case "panic":
133-
ev.setFunc(zap.NewAtomicLevelAt(zap.PanicLevel))
134-
case "fatal":
135-
ev.setFunc(zap.NewAtomicLevelAt(zap.FatalLevel))
136-
case "error":
137-
ev.setFunc(zap.NewAtomicLevelAt(zap.ErrorLevel))
138-
default:
125+
level, validLevel := levelStrings[strings.ToLower(flagValue)]
126+
if !validLevel {
139127
return fmt.Errorf("invalid stacktrace level \"%s\"", flagValue)
140128
}
129+
ev.setFunc(zap.NewAtomicLevelAt(level))
141130
ev.value = flagValue
142131
return nil
143132
}

pkg/log/zap/zap.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package zap
2020

2121
import (
2222
"flag"
23+
"fmt"
2324
"io"
2425
"os"
2526
"time"
@@ -213,8 +214,9 @@ func NewRaw(opts ...Opts) *zap.Logger {
213214
func (o *Options) BindFlags(fs *flag.FlagSet) {
214215

215216
// Set Development mode value
216-
fs.BoolVar(&o.Development, "zap-devel", false, "Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn)."+
217-
"If Development Mode is not set, defaults apply(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)")
217+
fs.BoolVar(&o.Development, "zap-devel", false,
218+
"Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn). "+
219+
"Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)")
218220

219221
// Set Encoder value
220222
var encVal encoderFlag
@@ -228,21 +230,24 @@ func (o *Options) BindFlags(fs *flag.FlagSet) {
228230
levelVal.setFunc = func(fromFlag zap.AtomicLevel) {
229231
o.Level = &fromFlag
230232
}
231-
fs.Var(&levelVal, "zap-log-level", "Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error',"+
232-
"or any integer value > 0 which corresponds to custom debug levels of increasing verbosity")
233+
fs.Var(&levelVal, "zap-log-level",
234+
"Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error', "+
235+
"or any integer value > 0 which corresponds to custom debug levels of increasing verbosity")
233236

234237
// Set the StrackTrace Level
235238
var stackVal stackTraceFlag
236239
stackVal.setFunc = func(fromFlag zap.AtomicLevel) {
237240
o.StacktraceLevel = &fromFlag
238241
}
239-
fs.Var(&stackVal, "zap-stacktrace-level", "Zap Level at and above which stacktraces are captured (one of 'warn' or 'error'")
242+
fs.Var(&stackVal, "zap-stacktrace-level",
243+
"Zap Level at and above which stacktraces are captured (one of 'warn' or 'error')")
240244
}
241245

242246
// UseFlagOptions to set logger with CLI passed flags.
243247
func UseFlagOptions(in *Options) Opts {
244248
return func(o *Options) {
245249
*o = *in
250+
fmt.Println("Passed Options are: ", in)
246251
o.addDefaults()
247252
}
248253
}

0 commit comments

Comments
 (0)