|
1 | 1 | // Copyright (c) The go-grpc-middleware Authors.
|
2 | 2 | // Licensed under the Apache License 2.0.
|
3 | 3 |
|
4 |
| -package log_test |
| 4 | +package examplelog_test |
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "bytes" |
7 | 8 | "context"
|
8 |
| - "fmt" |
9 | 9 | "log"
|
10 |
| - "os" |
| 10 | + "runtime" |
| 11 | + "strings" |
| 12 | + "testing" |
11 | 13 |
|
| 14 | + examplelog "github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples/log" |
12 | 15 | "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
|
| 16 | + "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | + "github.com/stretchr/testify/suite" |
13 | 20 | "google.golang.org/grpc"
|
14 | 21 | )
|
15 | 22 |
|
16 |
| -// InterceptorLogger adapts standard Go logger to interceptor logger. |
17 |
| -// This code is simple enough to be copied and not imported. |
18 |
| -func InterceptorLogger(l *log.Logger) logging.Logger { |
19 |
| - return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) { |
20 |
| - switch lvl { |
21 |
| - case logging.LevelDebug: |
22 |
| - msg = fmt.Sprintf("DEBUG :%v", msg) |
23 |
| - case logging.LevelInfo: |
24 |
| - msg = fmt.Sprintf("INFO :%v", msg) |
25 |
| - case logging.LevelWarn: |
26 |
| - msg = fmt.Sprintf("WARN :%v", msg) |
27 |
| - case logging.LevelError: |
28 |
| - msg = fmt.Sprintf("ERROR :%v", msg) |
29 |
| - default: |
30 |
| - panic(fmt.Sprintf("unknown level %v", lvl)) |
31 |
| - } |
32 |
| - l.Println(append([]any{"msg", msg}, fields...)) |
33 |
| - }) |
| 23 | +type logExampleTestSuite struct { |
| 24 | + *testpb.InterceptorTestSuite |
| 25 | + logBuffer *bytes.Buffer |
34 | 26 | }
|
35 | 27 |
|
36 |
| -func ExampleInterceptorLogger() { |
37 |
| - logger := log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile) |
| 28 | +func TestSuite(t *testing.T) { |
| 29 | + if strings.HasPrefix(runtime.Version(), "go1.7") { |
| 30 | + t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7") |
| 31 | + return |
| 32 | + } |
| 33 | + buffer := &bytes.Buffer{} |
| 34 | + logger := examplelog.InterceptorLogger(log.New(buffer, "", 0)) |
| 35 | + |
| 36 | + s := &logExampleTestSuite{ |
| 37 | + InterceptorTestSuite: &testpb.InterceptorTestSuite{ |
| 38 | + TestService: &testpb.TestPingService{}, |
| 39 | + }, |
| 40 | + logBuffer: buffer, |
| 41 | + } |
38 | 42 |
|
39 |
| - opts := []logging.Option{ |
40 |
| - logging.WithLogOnEvents(logging.StartCall, logging.FinishCall), |
41 |
| - // Add any other option (check functions starting with logging.With). |
| 43 | + s.InterceptorTestSuite.ServerOpts = []grpc.ServerOption{ |
| 44 | + grpc.StreamInterceptor(logging.StreamServerInterceptor(logger)), |
| 45 | + grpc.UnaryInterceptor(logging.UnaryServerInterceptor(logger)), |
42 | 46 | }
|
43 | 47 |
|
44 |
| - // You can now create a server with logging instrumentation that e.g. logs when the unary or stream call is started or finished. |
45 |
| - _ = grpc.NewServer( |
46 |
| - grpc.ChainUnaryInterceptor( |
47 |
| - logging.UnaryServerInterceptor(InterceptorLogger(logger), opts...), |
48 |
| - // Add any other interceptor you want. |
49 |
| - ), |
50 |
| - grpc.ChainStreamInterceptor( |
51 |
| - logging.StreamServerInterceptor(InterceptorLogger(logger), opts...), |
52 |
| - // Add any other interceptor you want. |
53 |
| - ), |
54 |
| - ) |
55 |
| - // ...user server. |
| 48 | + suite.Run(t, s) |
| 49 | +} |
| 50 | + |
| 51 | +func (s *logExampleTestSuite) TestPing() { |
| 52 | + ctx := context.Background() |
| 53 | + _, err := s.Client.Ping(ctx, testpb.GoodPing) |
| 54 | + assert.NoError(s.T(), err, "there must be not be an on a successful call") |
| 55 | + logStr := s.logBuffer.String() |
| 56 | + require.Contains(s.T(), logStr, "msg INFO") |
| 57 | + require.Contains(s.T(), logStr, ":started call") |
| 58 | + require.Contains(s.T(), logStr, "protocol grpc") |
| 59 | + require.Contains(s.T(), logStr, "grpc.component server") |
| 60 | + require.Contains(s.T(), logStr, "grpc.service testing.testpb.v1.TestService") |
| 61 | + require.Contains(s.T(), logStr, "grpc.method Ping") |
| 62 | + require.Contains(s.T(), logStr, "grpc.method_type unary") |
| 63 | + require.Contains(s.T(), logStr, "start_time ") |
| 64 | + require.Contains(s.T(), logStr, "grpc.time_ms") |
56 | 65 |
|
57 |
| - // Similarly you can create client that will log for the unary and stream client started or finished calls. |
58 |
| - _, _ = grpc.Dial( |
59 |
| - "some-target", |
60 |
| - grpc.WithChainUnaryInterceptor( |
61 |
| - logging.UnaryClientInterceptor(InterceptorLogger(logger), opts...), |
62 |
| - // Add any other interceptor you want. |
63 |
| - ), |
64 |
| - grpc.WithChainStreamInterceptor( |
65 |
| - logging.StreamClientInterceptor(InterceptorLogger(logger), opts...), |
66 |
| - // Add any other interceptor you want. |
67 |
| - ), |
68 |
| - ) |
69 |
| - // Output: |
70 | 66 | }
|
0 commit comments