Skip to content

Commit 9ed6d0a

Browse files
Flags supported added
1 parent 19e72eb commit 9ed6d0a

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/onsi/gomega v1.8.1
1414
github.com/prometheus/client_golang v1.0.0
1515
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90
16+
github.com/spf13/pflag v1.0.5
1617
go.uber.org/zap v1.10.0
1718
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
1819
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
@@ -22,6 +23,7 @@ require (
2223
k8s.io/apiextensions-apiserver v0.17.2
2324
k8s.io/apimachinery v0.17.2
2425
k8s.io/client-go v0.17.2
26+
k8s.io/klog v1.0.0
2527
k8s.io/utils v0.0.0-20191114184206-e782cd3c129f
2628
sigs.k8s.io/yaml v1.1.0
2729
)

pkg/log/zap/flags.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright 2019 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package zap
16+
17+
import (
18+
"flag"
19+
"fmt"
20+
"strconv"
21+
"strings"
22+
23+
"go.uber.org/zap"
24+
"go.uber.org/zap/zapcore"
25+
"k8s.io/klog"
26+
)
27+
28+
type encoderConfigFunc func(*zapcore.EncoderConfig)
29+
30+
type encoderValue struct {
31+
set bool
32+
newEncoder zapcore.Encoder
33+
str string
34+
}
35+
36+
func (v *encoderValue) Set(e string) error {
37+
v.set = true
38+
if e == "json" || e == "console" {
39+
v.newEncoder = newEncoder(e)
40+
} else {
41+
return fmt.Errorf("unknown encoder \"%s\"", e)
42+
}
43+
v.str = e
44+
return nil
45+
}
46+
47+
func (v encoderValue) String() string {
48+
return v.str
49+
}
50+
51+
func (v encoderValue) Type() string {
52+
return "encoder"
53+
}
54+
55+
func newEncoder(e string, ecfs ...encoderConfigFunc) zapcore.Encoder {
56+
if e == "json" {
57+
encoderConfig := zap.NewProductionEncoderConfig()
58+
for _, f := range ecfs {
59+
f(&encoderConfig)
60+
}
61+
fmt.Println("HERE WITH JSON")
62+
return zapcore.NewJSONEncoder(encoderConfig)
63+
} else {
64+
encoderConfig := zap.NewDevelopmentEncoderConfig()
65+
for _, f := range ecfs {
66+
f(&encoderConfig)
67+
}
68+
fmt.Println("HERE WITH CONSOLE")
69+
return zapcore.NewConsoleEncoder(encoderConfig)
70+
}
71+
72+
}
73+
74+
type levelValue struct {
75+
set bool
76+
level zap.AtomicLevel
77+
}
78+
79+
func (v *levelValue) Set(l string) error {
80+
v.set = true
81+
lower := strings.ToLower(l)
82+
var lvl int
83+
switch lower {
84+
case "debug":
85+
lvl = -1
86+
case "info":
87+
lvl = 0
88+
case "error":
89+
lvl = 2
90+
default:
91+
i, err := strconv.Atoi(lower)
92+
if err != nil {
93+
return fmt.Errorf("invalid log level \"%s\"", l)
94+
}
95+
96+
if i > 0 {
97+
lvl = -1 * i
98+
} else {
99+
return fmt.Errorf("NO LIKEY log level \"%s\"", l)
100+
}
101+
}
102+
v.level.SetLevel(zapcore.Level(int8(lvl)))
103+
// If log level is greater than debug, set glog/klog level to that level.
104+
if lvl < -3 {
105+
fs := flag.NewFlagSet("", flag.ContinueOnError)
106+
klog.InitFlags(fs)
107+
err := fs.Set("v", fmt.Sprintf("%v", -1*lvl))
108+
if err != nil {
109+
return err
110+
}
111+
}
112+
return nil
113+
}
114+
115+
func (v levelValue) String() string {
116+
return v.level.String()
117+
}
118+
119+
func (v levelValue) Type() string {
120+
return "level"
121+
}

pkg/log/zap/zap.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/go-logr/logr"
2727
"github.com/go-logr/zapr"
28+
"github.com/spf13/pflag"
2829
"go.uber.org/zap"
2930
"go.uber.org/zap/zapcore"
3031
)
@@ -207,3 +208,23 @@ func NewRaw(opts ...Opts) *zap.Logger {
207208
log = log.WithOptions(o.ZapOpts...)
208209
return log
209210
}
211+
212+
// BindToFlagSet
213+
func (o *Options) BindFlags(fs *pflag.FlagSet) {
214+
// Set Encoder value
215+
encval := encoderValue{newEncoder: o.Encoder}
216+
fs.Var(&encval, "encoder-value", "json||console")
217+
o.Encoder = encval.newEncoder
218+
219+
// Set the log level
220+
lv := levelValue{level: zap.NewAtomicLevel()}
221+
fs.Var(&lv, "log-level", "Log level")
222+
o.Level = &lv.level
223+
}
224+
225+
func UseNewOptions(in *Options) func(o *Options) {
226+
return func(o *Options) {
227+
*o = *in
228+
o.addDefaults()
229+
}
230+
}

0 commit comments

Comments
 (0)