44package logrus_test
55
66import (
7+ "bytes"
78 "context"
9+ "encoding/json"
810 "fmt"
11+ "runtime"
12+ "strings"
13+ "testing"
914
1015 "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
16+ "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
1117 "github.com/sirupsen/logrus"
18+ "github.com/stretchr/testify/assert"
19+ "github.com/stretchr/testify/require"
20+ "github.com/stretchr/testify/suite"
1221 "google.golang.org/grpc"
1322)
1423
@@ -18,11 +27,11 @@ func InterceptorLogger(l logrus.FieldLogger) logging.Logger {
1827 return logging .LoggerFunc (func (_ context.Context , lvl logging.Level , msg string , fields ... any ) {
1928 f := make (map [string ]any , len (fields )/ 2 )
2029 i := logging .Fields (fields ).Iterator ()
21- if i .Next () {
30+ for i .Next () {
2231 k , v := i .At ()
2332 f [k ] = v
2433 }
25- l : = l .WithFields (f )
34+ l = l .WithFields (f )
2635
2736 switch lvl {
2837 case logging .LevelDebug :
@@ -74,3 +83,54 @@ func ExampleInterceptorLogger() {
7483 )
7584 // Output:
7685}
86+
87+ type logrusExampleTestSuite struct {
88+ * testpb.InterceptorTestSuite
89+ logBuffer * bytes.Buffer
90+ }
91+
92+ func TestSuite (t * testing.T ) {
93+ if strings .HasPrefix (runtime .Version (), "go1.7" ) {
94+ t .Skipf ("Skipping due to json.RawMessage incompatibility with go1.7" )
95+ return
96+ }
97+
98+ buffer := & bytes.Buffer {}
99+ testLogger := logrus .New ()
100+ testLogger .Out = buffer
101+ testLogger .SetFormatter (& logrus.JSONFormatter {})
102+ logger := InterceptorLogger (testLogger )
103+
104+ s := & logrusExampleTestSuite {
105+ InterceptorTestSuite : & testpb.InterceptorTestSuite {
106+ TestService : & testpb.TestPingService {},
107+ },
108+ logBuffer : buffer ,
109+ }
110+
111+ s .InterceptorTestSuite .ServerOpts = []grpc.ServerOption {
112+ grpc .StreamInterceptor (logging .StreamServerInterceptor (logger , logging .WithLogOnEvents (logging .StartCall ))),
113+ grpc .UnaryInterceptor (logging .UnaryServerInterceptor (logger , logging .WithLogOnEvents (logging .StartCall ))),
114+ }
115+
116+ suite .Run (t , s )
117+ }
118+
119+ func (s * logrusExampleTestSuite ) TestPing () {
120+ ctx := context .Background ()
121+ _ , err := s .Client .Ping (ctx , testpb .GoodPing )
122+ assert .NoError (s .T (), err , "there must be not be an on a successful call" )
123+ var logMap map [string ]interface {}
124+ err = json .Unmarshal (s .logBuffer .Bytes (), & logMap )
125+ require .NoError (s .T (), err )
126+
127+ require .Equal (s .T (), "started call" , logMap ["msg" ])
128+ require .Equal (s .T (), "grpc" , logMap ["protocol" ])
129+ require .Equal (s .T (), "server" , logMap ["grpc.component" ])
130+ require .Equal (s .T (), "testing.testpb.v1.TestService" , logMap ["grpc.service" ])
131+ require .Equal (s .T (), "Ping" , logMap ["grpc.method" ])
132+ require .Equal (s .T (), "unary" , logMap ["grpc.method_type" ])
133+ require .Contains (s .T (), logMap ["peer.address" ], "127.0.0.1" )
134+ require .NotEmpty (s .T (), logMap ["grpc.start_time" ])
135+ require .NotEmpty (s .T (), logMap ["grpc.time_ms" ])
136+ }
0 commit comments