Skip to content

Commit

Permalink
Fix zap grpclogger verbosity check (#366)
Browse files Browse the repository at this point in the history
* Fix zap grpclogger verbosity check

Currently it only logs entries that are *lower* or equal to the
configured level, when you want the opposite. This means that by default
only INFO messages are logged, and anything more urgent is dropped.

* Add test verifying V method works as intended
  • Loading branch information
bouk authored Nov 28, 2020
1 parent c05cb40 commit 61b061e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion logging/zap/grpclogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,5 @@ func (l *zapGrpcLoggerV2) Fatalf(format string, args ...interface{}) {
}

func (l *zapGrpcLoggerV2) V(level int) bool {
return level <= l.verbosity
return l.verbosity <= level
}
33 changes: 33 additions & 0 deletions logging/zap/grpclogger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package grpc_zap

import (
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
"google.golang.org/grpc/grpclog"
)

func Test_zapGrpcLogger_V(t *testing.T) {
// copied from gRPC
const (
// infoLog indicates Info severity.
infoLog int = iota
// warningLog indicates Warning severity.
warningLog
// errorLog indicates Error severity.
errorLog
// fatalLog indicates Fatal severity.
fatalLog
)

core, _ := observer.New(zapcore.DebugLevel)
logger := zap.New(core)
ReplaceGrpcLoggerV2WithVerbosity(logger, warningLog)
assert.False(t, grpclog.V(infoLog))
assert.True(t, grpclog.V(warningLog))
assert.True(t, grpclog.V(errorLog))
assert.True(t, grpclog.V(fatalLog))
}

0 comments on commit 61b061e

Please sign in to comment.