Skip to content

Commit

Permalink
pkg/log/zap: clarify zap level vs. logr verbosity with link to zapr e…
Browse files Browse the repository at this point in the history
…xplanation and test cases

Signed-off-by: Eric Stroczynski <ericstroczynski@gmail.com>
  • Loading branch information
estroz committed Apr 19, 2021
1 parent 4aecab5 commit 5ed2a7c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
10 changes: 6 additions & 4 deletions pkg/log/zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ func newConsoleEncoder(opts ...EncoderConfigOption) zapcore.Encoder {
return zapcore.NewConsoleEncoder(encoderConfig)
}

// Level sets the the minimum enabled logging level e.g Debug, Info
// See Options.Level
// Level sets Options.Level, which configures the the minimum enabled logging level e.g Debug, Info.
// If using zapcore.Level(N), where N > 0, set zapcore.Level(-1*N) instead.
// See https://pkg.go.dev/github.com/go-logr/zapr for how zap level relates to logr verbosity.
func Level(level zapcore.LevelEnabler) func(o *Options) {
return func(o *Options) {
o.Level = level
Expand Down Expand Up @@ -151,8 +152,9 @@ type Options struct {
//
// Deprecated: Use DestWriter instead
DestWritter io.Writer
// Level configures the verbosity of the logging. Defaults to Debug when
// Development is true and Info otherwise
// Level configures the verbosity of the logging.
// Defaults to Debug when Development is true and Info otherwise.
// See https://pkg.go.dev/github.com/go-logr/zapr for how zap level relates to logr verbosity.
Level zapcore.LevelEnabler
// StacktraceLevel is the level at and above which stacktraces will
// be recorded for all messages. Defaults to Warn when Development
Expand Down
29 changes: 28 additions & 1 deletion pkg/log/zap/zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ var _ = Describe("Zap log level flag options setup", func() {
})
})

Context("with encoder options provided programmatically.", func() {
Context("with encoder options provided programmatically", func() {

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

Context("using Level()", func() {
var logOut *bytes.Buffer

BeforeEach(func() {
logOut = new(bytes.Buffer)
})

It("logs with negative logr level", func() {
By("setting up the logger")
logger := New(WriteTo(logOut), Level(zapcore.Level(-3)))
logger.V(3).Info("test 3") // Should be logged
Expect(string(logOut.Bytes())).To(ContainSubstring(`"msg":"test 3"`))
logOut.Truncate(0)
logger.V(4).Info("test 4") // Should not be logged
Expect(string(logOut.Bytes())).To(BeEmpty())
logger.V(-3).Info("test -3") // Log a panic, since V(-1*N) for all N > 0 is not permitted.
Expect(string(logOut.Bytes())).To(ContainSubstring(`"level":"dpanic"`))
})
It("does not log with positive logr level", func() {
By("setting up the logger")
logger := New(WriteTo(logOut), Level(zapcore.Level(1)))
logger.V(1).Info("test 1")
Expect(string(logOut.Bytes())).To(BeEmpty())
logger.V(3).Info("test 3")
Expect(string(logOut.Bytes())).To(BeEmpty())
})
})
})
})

0 comments on commit 5ed2a7c

Please sign in to comment.