4
4
package logrus_test
5
5
6
6
import (
7
+ "bytes"
7
8
"context"
9
+ "encoding/json"
8
10
"fmt"
11
+ "runtime"
12
+ "strings"
13
+ "testing"
9
14
10
15
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
16
+ "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
11
17
"github.com/sirupsen/logrus"
18
+ "github.com/stretchr/testify/assert"
19
+ "github.com/stretchr/testify/require"
20
+ "github.com/stretchr/testify/suite"
12
21
"google.golang.org/grpc"
13
22
)
14
23
@@ -18,7 +27,7 @@ func InterceptorLogger(l logrus.FieldLogger) logging.Logger {
18
27
return logging .LoggerFunc (func (_ context.Context , lvl logging.Level , msg string , fields ... any ) {
19
28
f := make (map [string ]any , len (fields )/ 2 )
20
29
i := logging .Fields (fields ).Iterator ()
21
- if i .Next () {
30
+ for i .Next () {
22
31
k , v := i .At ()
23
32
f [k ] = v
24
33
}
@@ -74,3 +83,54 @@ func ExampleInterceptorLogger() {
74
83
)
75
84
// Output:
76
85
}
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