Skip to content

Commit 3156051

Browse files
Fix zap levels to align with logr
1 parent eb68324 commit 3156051

File tree

2 files changed

+148
-13
lines changed

2 files changed

+148
-13
lines changed

pkg/log/zap/flags.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@ import (
2929
)
3030

3131
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,
32+
"debug": zap.DebugLevel,
33+
"info": zap.InfoLevel,
34+
"error": zap.ErrorLevel,
35+
}
36+
37+
var stackLevelStrings = map[string]zapcore.Level{
38+
"disabled": zap.FatalLevel,
39+
"info": zap.InfoLevel,
40+
"error": zap.ErrorLevel,
4241
}
4342

4443
type encoderFlag struct {
@@ -94,14 +93,15 @@ func (ev *levelFlag) Set(flagValue string) error {
9493
if err != nil {
9594
return fmt.Errorf("invalid log level \"%s\"", flagValue)
9695
}
97-
if logLevel > 0 {
96+
if logLevel >= 0 {
9897
intLevel := -1 * logLevel
9998
ev.setFunc(zap.NewAtomicLevelAt(zapcore.Level(int8(intLevel))))
10099
} else {
101100
return fmt.Errorf("invalid log level \"%s\"", flagValue)
102101
}
102+
} else {
103+
ev.setFunc(zap.NewAtomicLevelAt(level))
103104
}
104-
ev.setFunc(zap.NewAtomicLevelAt(level))
105105
ev.value = flagValue
106106
return nil
107107
}
@@ -122,7 +122,7 @@ type stackTraceFlag struct {
122122
var _ pflag.Value = &stackTraceFlag{}
123123

124124
func (ev *stackTraceFlag) Set(flagValue string) error {
125-
level, validLevel := levelStrings[strings.ToLower(flagValue)]
125+
level, validLevel := stackLevelStrings[strings.ToLower(flagValue)]
126126
if !validLevel {
127127
return fmt.Errorf("invalid stacktrace level \"%s\"", flagValue)
128128
}

pkg/log/zap/zap_test.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ package zap
1919
import (
2020
"bytes"
2121
"encoding/json"
22+
"flag"
2223
"io/ioutil"
24+
"os"
2325

2426
"github.com/go-logr/logr"
2527
. "github.com/onsi/ginkgo"
2628
. "github.com/onsi/gomega"
29+
"go.uber.org/zap/zapcore"
2730
kapi "k8s.io/api/core/v1"
2831
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2932
"k8s.io/apimachinery/pkg/types"
@@ -296,3 +299,135 @@ var _ = Describe("Zap logger setup", func() {
296299
})
297300
})
298301
})
302+
303+
var _ = Describe("Zap log level flag options setup", func() {
304+
var (
305+
fromFlags Options
306+
fs flag.FlagSet
307+
)
308+
309+
BeforeEach(func() {
310+
fromFlags = Options{}
311+
fs = *flag.NewFlagSet(os.Args[0], flag.ExitOnError) //flags are now reset
312+
})
313+
314+
Context("with zap-log-level options provided", func() {
315+
It("Should set info/debug zap-log-level flags.", func() {
316+
args := []string{"--zap-log-level=debug"}
317+
fromFlags.BindFlags(&fs)
318+
if err := fs.Parse(args); err != nil {
319+
Expect(err).ToNot(HaveOccurred())
320+
}
321+
logOut := new(bytes.Buffer)
322+
323+
logger := New(UseFlagOptions(&fromFlags), WriteTo(logOut))
324+
logger.V(0).Info("info text")
325+
logger.V(1).Info("debug 1 text")
326+
327+
outRaw := logOut.Bytes()
328+
329+
Expect(string(outRaw)).Should(ContainSubstring("info text"))
330+
Expect(string(outRaw)).Should(ContainSubstring("debug 1 text"))
331+
332+
})
333+
334+
It("Should set error zap-log-level flags.", func() {
335+
args := []string{"--zap-log-level=error"}
336+
fromFlags.BindFlags(&fs)
337+
if err := fs.Parse(args); err != nil {
338+
Expect(err).ToNot(HaveOccurred())
339+
}
340+
logOut := new(bytes.Buffer)
341+
342+
logger := New(UseFlagOptions(&fromFlags), WriteTo(logOut))
343+
logger.V(0).Info("info text")
344+
logger.V(1).Info("debug 1 text")
345+
346+
outRaw := logOut.Bytes()
347+
348+
Expect(outRaw).To(BeEmpty())
349+
})
350+
351+
It("Should set level 1 debug zap-log-level flags.", func() {
352+
args := []string{"--zap-log-level=1"}
353+
fromFlags.BindFlags(&fs)
354+
if err := fs.Parse(args); err != nil {
355+
Expect(err).ToNot(HaveOccurred())
356+
}
357+
logOut := new(bytes.Buffer)
358+
359+
logger := New(UseFlagOptions(&fromFlags), WriteTo(logOut))
360+
logger.V(0).Info("info text")
361+
logger.V(1).Info("debug 1 text")
362+
363+
outRaw := logOut.Bytes()
364+
365+
Expect(string(outRaw)).Should(ContainSubstring("info text"))
366+
Expect(string(outRaw)).Should(ContainSubstring("debug 1 text"))
367+
})
368+
})
369+
370+
Context("with zap-log-level with increased verbosity.", func() {
371+
It("Should set Level 2 increased verbosity for zap-log-level flags.", func() {
372+
args := []string{"--zap-log-level=2"}
373+
fromFlags.BindFlags(&fs)
374+
if err := fs.Parse(args); err != nil {
375+
Expect(err).ToNot(HaveOccurred())
376+
}
377+
logOut := new(bytes.Buffer)
378+
379+
logger := New(UseFlagOptions(&fromFlags), WriteTo(logOut))
380+
logger.V(0).Info("info text")
381+
logger.V(1).Info("debug 1 text")
382+
logger.V(2).Info("debug 2 text")
383+
384+
outRaw := logOut.Bytes()
385+
386+
Expect(string(outRaw)).Should(ContainSubstring("info text"))
387+
Expect(string(outRaw)).Should(ContainSubstring("debug 1 text"))
388+
Expect(string(outRaw)).Should(ContainSubstring("debug 2 text"))
389+
390+
})
391+
392+
})
393+
394+
Context("with zap-stacktrace-level options provided", func() {
395+
396+
It("Should set info stacktrace for zap-stacktrace-level flags.", func() {
397+
args := []string{"--zap-stacktrace-level=info", "--zap-devel=true"}
398+
fromFlags.BindFlags(&fs)
399+
if err := fs.Parse(args); err != nil {
400+
Expect(err).ToNot(HaveOccurred())
401+
}
402+
out := Options{}
403+
UseFlagOptions(&fromFlags)(&out)
404+
405+
Expect(out.StacktraceLevel.Enabled(zapcore.InfoLevel)).To(BeTrue())
406+
})
407+
408+
It("Should set error stacktrace for zap-stacktrace-level flags.", func() {
409+
args := []string{"--zap-stacktrace-level=error", "--zap-devel=true"}
410+
fromFlags.BindFlags(&fs)
411+
if err := fs.Parse(args); err != nil {
412+
Expect(err).ToNot(HaveOccurred())
413+
}
414+
out := Options{}
415+
UseFlagOptions(&fromFlags)(&out)
416+
417+
Expect(out.StacktraceLevel.Enabled(zapcore.ErrorLevel)).To(BeTrue())
418+
})
419+
420+
It("Should disable stacktrace for zap-stacktrace-level flags.", func() {
421+
args := []string{"--zap-stacktrace-level=disabled", "--zap-devel=true"}
422+
fromFlags.BindFlags(&fs)
423+
if err := fs.Parse(args); err != nil {
424+
Expect(err).ToNot(HaveOccurred())
425+
}
426+
out := Options{}
427+
UseFlagOptions(&fromFlags)(&out)
428+
429+
Expect(out.StacktraceLevel.Enabled(zapcore.FatalLevel)).To(BeTrue())
430+
})
431+
})
432+
433+
})

0 commit comments

Comments
 (0)