Skip to content

Commit 7943096

Browse files
Working flags added
1 parent c9a71ba commit 7943096

File tree

2 files changed

+36
-46
lines changed

2 files changed

+36
-46
lines changed

pkg/log/zap/flags.go

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import (
2525
type encoderConfigFunc func(*zapcore.EncoderConfig)
2626

2727
type encoderValue struct {
28-
set bool
29-
newEncoder zapcore.Encoder
30-
str string
28+
set bool
29+
setFunc func(zapcore.Encoder)
30+
str string
3131
}
3232

3333
func (v encoderValue) String() string {
@@ -43,13 +43,9 @@ func (v *encoderValue) Set(e string) error {
4343
val := strings.ToLower(e)
4444
switch val {
4545
case "json":
46-
v.newEncoder = newJSONEncoder()
47-
fmt.Printf("got JSON : %p \n", v.newEncoder)
48-
46+
v.setFunc(newJSONEncoder())
4947
case "console":
50-
v.newEncoder = newConsoleEncoder()
51-
fmt.Printf("got CONSOLE : %p \n", v.newEncoder)
52-
48+
v.setFunc(newConsoleEncoder())
5349
default:
5450
return fmt.Errorf("invalid encoder value \"%s\"", e)
5551
}
@@ -74,62 +70,58 @@ func newConsoleEncoder(ecfs ...encoderConfigFunc) zapcore.Encoder {
7470
}
7571

7672
type levelValue struct {
77-
set bool
78-
level zap.AtomicLevel
73+
set bool
74+
setFunc func(zap.AtomicLevel)
75+
str string
7976
}
8077

8178
func (v *levelValue) Set(l string) error {
8279
v.set = true
8380
lower := strings.ToLower(l)
84-
var lvl zap.AtomicLevel
8581
switch lower {
8682
case "debug":
87-
lvl = zap.NewAtomicLevelAt(zap.DebugLevel)
83+
v.setFunc(zap.NewAtomicLevelAt(zap.DebugLevel))
8884
case "info":
89-
lvl = zap.NewAtomicLevelAt(zap.InfoLevel)
85+
v.setFunc(zap.NewAtomicLevelAt(zap.InfoLevel))
9086
default:
9187
return fmt.Errorf("invalid log level \"%s\"", l)
9288
}
93-
//v.level.SetLevel(zapcore.Level(int8(lvl)))
94-
v.level = lvl
95-
fmt.Println("*******", v.level)
96-
89+
v.str = l
9790
return nil
9891
}
9992

10093
func (v levelValue) String() string {
101-
return v.level.String()
94+
return v.str
10295
}
10396

10497
func (v levelValue) Type() string {
10598
return "level"
10699
}
107100

108101
type stackTraceValue struct {
109-
set bool
110-
lv zap.AtomicLevel
102+
set bool
103+
setFunc func(zap.AtomicLevel)
104+
str string
111105
}
112106

113107
func (s *stackTraceValue) Set(val string) error {
114108
s.set = true
115109
lower := strings.ToLower(val)
116-
//var lv1 zap.AtomicLevel
117110
switch lower {
118111
case "warn":
119-
s.lv = zap.NewAtomicLevelAt(zap.WarnLevel)
112+
s.setFunc(zap.NewAtomicLevelAt(zap.WarnLevel))
120113
case "error":
121-
s.lv = zap.NewAtomicLevelAt(zap.ErrorLevel)
114+
s.setFunc(zap.NewAtomicLevelAt(zap.ErrorLevel))
122115
default:
123116
return fmt.Errorf("invalid stacktrace level \"%s\"", val)
124117
}
125-
//s.lv = lv1
126118
return nil
127119
}
128120

129121
func (s stackTraceValue) String() string {
130-
return s.lv.String()
122+
return s.str
131123
}
132124

133125
func (_ stackTraceValue) Type() string {
134-
return "lv"
126+
return "level"
135127
}

pkg/log/zap/zap.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ limitations under the License.
1919
package zap
2020

2121
import (
22-
"fmt"
2322
"io"
2423
"os"
2524
"time"
@@ -217,31 +216,30 @@ func (o *Options) BindFlags(fs *pflag.FlagSet) {
217216

218217
// Set Encoder value
219218
var encVal encoderValue
220-
//encVal := encoderValue{newEncoder: o.Encoder}
219+
encVal.setFunc = func(fromFlag zapcore.Encoder) {
220+
o.Encoder = fromFlag
221+
}
221222
fs.Var(&encVal, "zap-encoder", "Zap log encoding ('json' or 'console')")
222-
o.Encoder = encVal.newEncoder
223-
fmt.Printf("got o.Encoder : %p \n", o.Encoder)
224-
fmt.Printf("got newEncoder : %p \n", encVal.newEncoder)
225-
226-
// Set the log level
227-
var lv levelValue
228-
//lv := levelValue{level: zap.NewAtomicLevel()}
229-
fs.Var(&lv, "zap-log-level", "Zap log level (one of 'debug', 'info', 'error' or any integer value > 0)")
230-
o.Level = &lv.level
231-
fmt.Println("LOG LEVEL IS : ", o.Level)
232-
233-
/* Set the log level
234-
stackVal := stackTraceValue{lv: zap.NewAtomicLevel()}
223+
224+
// Set the Log Level
225+
var levelVal levelValue
226+
levelVal.setFunc = func(fromFlag zap.AtomicLevel) {
227+
o.Level = &fromFlag
228+
}
229+
fs.Var(&levelVal, "zap-log-level", "Zap log level (one of 'debug', 'info', 'error' or any integer value > 0)")
230+
231+
// Set the StrackTrace Level
232+
var stackVal stackTraceValue
233+
stackVal.setFunc = func(fromFlag zap.AtomicLevel) {
234+
o.StacktraceLevel = &fromFlag
235+
}
235236
fs.Var(&stackVal, "zap-stacktrace-level", "Zap log level (one of 'warn', 'error'")
236-
o.StacktraceLevel = &stackVal.lv
237-
fmt.Println("STACKTRACE VALUE IS :", o.StacktraceLevel)*/
238237

239238
}
240239

241-
func UseNewOptions(in *Options) func(o *Options) {
240+
func UseFlagOptions(in *Options) func(o *Options) {
242241
return func(o *Options) {
243242
*o = *in
244243
o.addDefaults()
245-
fmt.Println("FINAL OPTS: ", o)
246244
}
247245
}

0 commit comments

Comments
 (0)