Skip to content

Commit 7aaebcf

Browse files
committed
pkg/log/zap: clarify zap level vs. logr verbosity with link to zapr explanation and test cases
Signed-off-by: Eric Stroczynski <ericstroczynski@gmail.com>
1 parent 4aecab5 commit 7aaebcf

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

pkg/log/zap/zap.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,19 @@ func newConsoleEncoder(opts ...EncoderConfigOption) zapcore.Encoder {
101101
return zapcore.NewConsoleEncoder(encoderConfig)
102102
}
103103

104-
// Level sets the the minimum enabled logging level e.g Debug, Info
105-
// See Options.Level
104+
// Level sets Options.Level, which configures the the minimum enabled logging level e.g Debug, Info.
105+
// A zap log level should be multiplied by -1 to get the logr verbosity.
106+
// For example, to get logr verbosity of 3, pass zapcore.Level(-3) to this Opts.
107+
// See https://pkg.go.dev/github.com/go-logr/zapr for how zap level relates to logr verbosity.
106108
func Level(level zapcore.LevelEnabler) func(o *Options) {
107109
return func(o *Options) {
108110
o.Level = level
109111
}
110112
}
111113

112-
// StacktraceLevel configures the logger to record a stack trace for all messages at
113-
// or above a given level.
114-
// See Options.StacktraceLevel
114+
// StacktraceLevel sets Options.StacktraceLevel, which configures the logger to record a stack trace
115+
// for all messages at or above a given level.
116+
// See the Level Opts for the relationship of zap log level to logr verbosity.
115117
func StacktraceLevel(stacktraceLevel zapcore.LevelEnabler) func(o *Options) {
116118
return func(o *Options) {
117119
o.StacktraceLevel = stacktraceLevel
@@ -151,12 +153,16 @@ type Options struct {
151153
//
152154
// Deprecated: Use DestWriter instead
153155
DestWritter io.Writer
154-
// Level configures the verbosity of the logging. Defaults to Debug when
155-
// Development is true and Info otherwise
156+
// Level configures the verbosity of the logging.
157+
// Defaults to Debug when Development is true and Info otherwise.
158+
// A zap log level should be multiplied by -1 to get the logr verbosity.
159+
// For example, to get logr verbosity of 3, set this field to zapcore.Level(-3).
160+
// See https://pkg.go.dev/github.com/go-logr/zapr for how zap level relates to logr verbosity.
156161
Level zapcore.LevelEnabler
157162
// StacktraceLevel is the level at and above which stacktraces will
158163
// be recorded for all messages. Defaults to Warn when Development
159-
// is true and Error otherwise
164+
// is true and Error otherwise.
165+
// See Level for the relationship of zap log level to logr verbosity.
160166
StacktraceLevel zapcore.LevelEnabler
161167
// ZapOpts allows passing arbitrary zap.Options to configure on the
162168
// underlying Zap logger.

pkg/log/zap/zap_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ var _ = Describe("Zap log level flag options setup", func() {
472472
})
473473
})
474474

475-
Context("with encoder options provided programmatically.", func() {
475+
Context("with encoder options provided programmatically", func() {
476476

477477
It("Should set Console Encoder, with given Nanos TimeEncoder option.", func() {
478478
logOut := new(bytes.Buffer)
@@ -517,5 +517,35 @@ var _ = Describe("Zap log level flag options setup", func() {
517517
Expect(string(outRaw)).Should(ContainSubstring("MillisTimeFormat"))
518518
})
519519

520+
Context("using Level()", func() {
521+
var logOut *bytes.Buffer
522+
523+
BeforeEach(func() {
524+
logOut = new(bytes.Buffer)
525+
})
526+
527+
It("logs with negative logr level", func() {
528+
By("setting up the logger")
529+
logger := New(WriteTo(logOut), Level(zapcore.Level(-3)))
530+
logger.V(3).Info("test 3") // Should be logged
531+
Expect(string(logOut.Bytes())).To(ContainSubstring(`"msg":"test 3"`))
532+
logOut.Truncate(0)
533+
logger.V(1).Info("test 1") // Should be logged
534+
Expect(string(logOut.Bytes())).To(ContainSubstring(`"msg":"test 1"`))
535+
logOut.Truncate(0)
536+
logger.V(4).Info("test 4") // Should not be logged
537+
Expect(string(logOut.Bytes())).To(BeEmpty())
538+
logger.V(-3).Info("test -3") // Log a panic, since V(-1*N) for all N > 0 is not permitted.
539+
Expect(string(logOut.Bytes())).To(ContainSubstring(`"level":"dpanic"`))
540+
})
541+
It("does not log with positive logr level", func() {
542+
By("setting up the logger")
543+
logger := New(WriteTo(logOut), Level(zapcore.Level(1)))
544+
logger.V(1).Info("test 1")
545+
Expect(string(logOut.Bytes())).To(BeEmpty())
546+
logger.V(3).Info("test 3")
547+
Expect(string(logOut.Bytes())).To(BeEmpty())
548+
})
549+
})
520550
})
521551
})

0 commit comments

Comments
 (0)