Skip to content

Commit

Permalink
fix zapgrpc V function
Browse files Browse the repository at this point in the history
  • Loading branch information
kazegusuri committed Aug 30, 2018
1 parent 0e55ec4 commit 7b7e0f6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions zapgrpc/zapgrpc.go
Expand Up @@ -23,7 +23,6 @@ package zapgrpc // import "go.uber.org/zap/zapgrpc"

import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// An Option overrides a Logger's default configuration.
Expand All @@ -46,6 +45,13 @@ func WithDebug() Option {
})
}

// WithVerbosity sets verbose level referred by V().
func WithVerbosity(v int) Option {
return optionFunc(func(logger *Logger) {
logger.v = v
})
}

// NewLogger returns a new Logger.
//
// By default, Loggers print at zap's InfoLevel.
Expand All @@ -71,6 +77,7 @@ func NewLogger(l *zap.Logger, options ...Option) *Logger {

// Logger adapts zap's Logger to be compatible with grpclog.Logger and grpclog.LoggerV2.
type Logger struct {
v int
log *zap.SugaredLogger
info func(*zap.SugaredLogger, ...interface{})
infof func(*zap.SugaredLogger, string, ...interface{})
Expand Down Expand Up @@ -161,5 +168,5 @@ func (l *Logger) Println(args ...interface{}) {

// V implements grpclog.LoggerV2.
func (l *Logger) V(lvl int) bool {
return l.log.Desugar().Core().Enabled(zapcore.Level(lvl))
return lvl <= l.v
}
4 changes: 2 additions & 2 deletions zapgrpc/zapgrpc_test.go
Expand Up @@ -105,13 +105,13 @@ func TestLoggerFatalExpected(t *testing.T) {
}

func TestLoggerTrueExpected(t *testing.T) {
checkLevel(t, zapcore.FatalLevel, true, func(logger *Logger) bool {
checkLevel(t, zapcore.FatalLevel, false, func(logger *Logger) bool {
return logger.V(6)
})
}

func TestLoggerFalseExpected(t *testing.T) {
checkLevel(t, zapcore.FatalLevel, false, func(logger *Logger) bool {
checkLevel(t, zapcore.FatalLevel, true, func(logger *Logger) bool {
return logger.V(0)
})
}
Expand Down

0 comments on commit 7b7e0f6

Please sign in to comment.