Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ pkg/log/zap: clarify zap level vs. logr verbosity #1485

Merged
merged 1 commit into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 14 additions & 8 deletions pkg/log/zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,19 @@ 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.
// A zap log level should be multiplied by -1 to get the logr verbosity.
// For example, to get logr verbosity of 3, pass zapcore.Level(-3) to this Opts.
// 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
}
}

// StacktraceLevel configures the logger to record a stack trace for all messages at
// or above a given level.
// See Options.StacktraceLevel
// StacktraceLevel sets Options.StacktraceLevel, which configures the logger to record a stack trace
// for all messages at or above a given level.
// See the Level Opts for the relationship of zap log level to logr verbosity.
func StacktraceLevel(stacktraceLevel zapcore.LevelEnabler) func(o *Options) {
return func(o *Options) {
o.StacktraceLevel = stacktraceLevel
Expand Down Expand Up @@ -151,12 +153,16 @@ 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.
// A zap log level should be multiplied by -1 to get the logr verbosity.
// For example, to get logr verbosity of 3, set this field to zapcore.Level(-3).
// 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
// is true and Error otherwise
// is true and Error otherwise.
// See Level for the relationship of zap log level to logr verbosity.
StacktraceLevel zapcore.LevelEnabler
// ZapOpts allows passing arbitrary zap.Options to configure on the
// underlying Zap logger.
Expand Down
32 changes: 31 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,35 @@ 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(1).Info("test 1") // Should be logged
Expect(string(logOut.Bytes())).To(ContainSubstring(`"msg":"test 1"`))
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())
})
})
})
})