From 83ed827d3ce088a87b607396c2885fd4104d61f7 Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 1 Dec 2020 17:11:57 +0700 Subject: [PATCH] Fix logrus verbosity level checker (#369) * fix verbosity checker * add test for verbosity checker --- logging/logrus/grpclogger.go | 2 +- logging/logrus/grpclogger_test.go | 71 +++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 logging/logrus/grpclogger_test.go diff --git a/logging/logrus/grpclogger.go b/logging/logrus/grpclogger.go index 84dacf4ef..374348e82 100644 --- a/logging/logrus/grpclogger.go +++ b/logging/logrus/grpclogger.go @@ -21,5 +21,5 @@ type logrusGrpcLoggerV2 struct { } func (l *logrusGrpcLoggerV2) V(level int) bool { - return int(l.Level) >= level + return l.Logger.IsLevelEnabled(logrus.Level(level)) } diff --git a/logging/logrus/grpclogger_test.go b/logging/logrus/grpclogger_test.go new file mode 100644 index 000000000..ffecd22f0 --- /dev/null +++ b/logging/logrus/grpclogger_test.go @@ -0,0 +1,71 @@ +package grpc_logrus + +import ( + "testing" + + "github.com/sirupsen/logrus" +) + +func Test_logrusGrpcLoggerV2_V(t *testing.T) { + tests := []struct { + name string + setupLevel logrus.Level + inLevel logrus.Level + want bool + }{ + { + name: "WarnLevel setup when we have WarnLevel msg should return TRUE", + setupLevel: logrus.WarnLevel, + inLevel: logrus.WarnLevel, + want: true, + }, + { + name: "WarnLevel setup when we have ErrorLevel msg should return TRUE", + setupLevel: logrus.WarnLevel, + inLevel: logrus.ErrorLevel, + want: true, + }, + { + name: "WarnLevel setup when we have InfoLevel msg should return FALSE", + setupLevel: logrus.WarnLevel, + inLevel: logrus.InfoLevel, + want: false, + }, + { + name: "WarnLevel setup when we have DebugLevel msg should return FALSE", + setupLevel: logrus.WarnLevel, + inLevel: logrus.DebugLevel, + want: false, + }, + { + name: "WarnLevel setup when we have TraceLevel msg should return FALSE", + setupLevel: logrus.WarnLevel, + inLevel: logrus.TraceLevel, + want: false, + }, + { + name: "TraceLevel setup when we have WarnLevel msg should return TRUE", + setupLevel: logrus.TraceLevel, + inLevel: logrus.WarnLevel, + want: true, + }, + { + name: "TraceLevel setup when we have TraceLevel msg should return TRUE", + setupLevel: logrus.TraceLevel, + inLevel: logrus.TraceLevel, + want: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + lr := logrus.New() + lr.SetLevel(tt.setupLevel) + + l := &logrusGrpcLoggerV2{logrus.NewEntry(lr)} + + if got := l.V(int(tt.inLevel)); got != tt.want { + t.Errorf("V() = %v, want %v", got, tt.want) + } + }) + } +}