Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions zapgrpc/zapgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func WithDebug() Option {
})
}

// WithVerbosity configures a Logger to use the given verbosity level for V() calls.
// By default, V() uses zap's severity levels instead (Info, Warn, Error, Fatal).
func WithVerbosity(verbosity int) Option {
return optionFunc(func(logger *Logger) {
logger.useVerbosity = true
logger.verbosity = verbosity
})
}

// withWarn redirects the fatal level to the warn level, which makes testing
// easier. This is intentionally unexported.
func withWarn() Option {
Expand Down Expand Up @@ -140,8 +149,8 @@ type Logger struct {
levelEnabler zapcore.LevelEnabler
print *printer
fatal *printer
// printToDebug bool
// fatalToWarn bool
useVerbosity bool
verbosity int
}

// Print implements grpclog.Logger.
Expand Down Expand Up @@ -233,6 +242,9 @@ func (l *Logger) Fatalf(format string, args ...interface{}) {

// V implements grpclog.LoggerV2.
func (l *Logger) V(level int) bool {
if l.useVerbosity {
return level <= l.verbosity
}
return l.levelEnabler.Enabled(_grpcToZapLevel[level])
}

Expand Down
53 changes: 52 additions & 1 deletion zapgrpc/zapgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,17 @@ func checkLevel(
expectedBool bool,
f func(*Logger) bool,
) {
withLogger(enab, nil, func(logger *Logger, observedLogs *observer.ObservedLogs) {
checkLevelOpts(t, enab, nil, expectedBool, f)
}

func checkLevelOpts(
t testing.TB,
enab zapcore.LevelEnabler,
opts []Option,
expectedBool bool,
f func(*Logger) bool,
) {
withLogger(enab, opts, func(logger *Logger, observedLogs *observer.ObservedLogs) {
actualBool := f(logger)
if expectedBool {
require.True(t, actualBool)
Expand All @@ -231,6 +241,47 @@ func checkLevel(
})
}

func TestLoggerVWithVerbosity(t *testing.T) {
tests := []struct {
verbosity int
grpcEnabled []int
grpcDisabled []int
}{
{
verbosity: 0,
grpcEnabled: []int{0, -1},
grpcDisabled: []int{1, 2, 3},
},
{
verbosity: 2,
grpcEnabled: []int{0, 1, 2},
grpcDisabled: []int{3, 4},
},
}

// Test that it behaves independently of the core zap level
zapLevels := []zapcore.Level{zapcore.DebugLevel, zapcore.InfoLevel, zapcore.ErrorLevel}

for _, zl := range zapLevels {
for _, tst := range tests {
for _, grpcLvl := range tst.grpcEnabled {
t.Run(fmt.Sprintf("level %s verbosity %d enabled %d", zl, tst.verbosity, grpcLvl), func(t *testing.T) {
checkLevelOpts(t, zl, []Option{WithVerbosity(tst.verbosity)}, true, func(logger *Logger) bool {
return logger.V(grpcLvl)
})
})
}
for _, grpcLvl := range tst.grpcDisabled {
t.Run(fmt.Sprintf("level %s verbosity %d disabled %d", zl, tst.verbosity, grpcLvl), func(t *testing.T) {
checkLevelOpts(t, zl, []Option{WithVerbosity(tst.verbosity)}, false, func(logger *Logger) bool {
return logger.V(grpcLvl)
})
})
}
}
}
}

func checkMessages(
t testing.TB,
enab zapcore.LevelEnabler,
Expand Down