|
| 1 | +package phuslog_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + grpcphuslog "github.com/grpc-ecosystem/go-grpc-middleware/providers/phuslog/v2" |
| 8 | + "github.com/phuslu/log" |
| 9 | + "google.golang.org/grpc" |
| 10 | + |
| 11 | + middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2" |
| 12 | + "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" |
| 13 | + "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/tags" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + customFunc logging.CodeToLevel |
| 18 | + customDurationToFields logging.DurationToFields |
| 19 | +) |
| 20 | + |
| 21 | +func Example_initializationWithCustomLevels() { |
| 22 | + // Logger is used, allowing pre-definition of certain fields by the user. |
| 23 | + logger := log.DefaultLogger.GrcpGateway() |
| 24 | + // Shared options for the logger, with a custom gRPC code to log level function. |
| 25 | + opts := []logging.Option{ |
| 26 | + logging.WithLevels(customFunc), |
| 27 | + } |
| 28 | + // Create a server, make sure we put the tags context before everything else. |
| 29 | + _ = grpc.NewServer( |
| 30 | + middleware.WithUnaryServerChain( |
| 31 | + tags.UnaryServerInterceptor(), |
| 32 | + logging.UnaryServerInterceptor(grpcphuslog.InterceptorLogger(logger), opts...), |
| 33 | + ), |
| 34 | + middleware.WithStreamServerChain( |
| 35 | + tags.StreamServerInterceptor(), |
| 36 | + logging.StreamServerInterceptor(grpcphuslog.InterceptorLogger(logger), opts...), |
| 37 | + ), |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +func Example_initializationWithDurationFieldOverride() { |
| 42 | + // Logger is used, allowing pre-definition of certain fields by the user. |
| 43 | + logger := log.DefaultLogger.GrcpGateway() |
| 44 | + // Shared options for the logger, with a custom duration to log field function. |
| 45 | + opts := []logging.Option{ |
| 46 | + logging.WithDurationField(customDurationToFields), |
| 47 | + } |
| 48 | + // Create a server, make sure we put the tags context before everything else. |
| 49 | + _ = grpc.NewServer( |
| 50 | + middleware.WithUnaryServerChain( |
| 51 | + tags.UnaryServerInterceptor(), |
| 52 | + logging.UnaryServerInterceptor(grpcphuslog.InterceptorLogger(logger), opts...), |
| 53 | + ), |
| 54 | + middleware.WithStreamServerChain( |
| 55 | + tags.StreamServerInterceptor(), |
| 56 | + logging.StreamServerInterceptor(grpcphuslog.InterceptorLogger(logger), opts...), |
| 57 | + ), |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +func Example_initializationWithCodeGenRequestFieldExtractor() { |
| 62 | + // Logger is used, allowing pre-definition of certain fields by the user. |
| 63 | + logger := log.DefaultLogger.GrcpGateway() |
| 64 | + // Create a server, make sure we put the tags context before everything else. |
| 65 | + _ = grpc.NewServer( |
| 66 | + middleware.WithUnaryServerChain( |
| 67 | + tags.UnaryServerInterceptor(tags.WithFieldExtractor(tags.CodeGenRequestFieldExtractor)), |
| 68 | + logging.UnaryServerInterceptor(grpcphuslog.InterceptorLogger(logger)), |
| 69 | + ), |
| 70 | + middleware.WithStreamServerChain( |
| 71 | + tags.StreamServerInterceptor(tags.WithFieldExtractor(tags.CodeGenRequestFieldExtractor)), |
| 72 | + logging.StreamServerInterceptor(grpcphuslog.InterceptorLogger(logger)), |
| 73 | + ), |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +func ExampleWithDecider() { |
| 78 | + // Logger is used, allowing pre-definition of certain fields by the user. |
| 79 | + logger := log.DefaultLogger.GrcpGateway() |
| 80 | + // Shared options for the logger, with a custom decider that log everything except successful |
| 81 | + // calls from "/blah.foo.healthcheck/Check" method. |
| 82 | + opts := []logging.Option{ |
| 83 | + logging.WithDecider(func(methodFullName string) logging.Decision { |
| 84 | + // will not log gRPC calls if it was a call to healthcheck and no error was raised |
| 85 | + if methodFullName == "/blah.foo.healthcheck/Check" { |
| 86 | + return logging.NoLogCall |
| 87 | + } |
| 88 | + |
| 89 | + // by default you will log all calls |
| 90 | + return logging.LogStartAndFinishCall |
| 91 | + }), |
| 92 | + } |
| 93 | + // Create a server, make sure we put the tags context before everything else. |
| 94 | + _ = []grpc.ServerOption{ |
| 95 | + middleware.WithUnaryServerChain( |
| 96 | + tags.UnaryServerInterceptor(), |
| 97 | + logging.UnaryServerInterceptor(grpcphuslog.InterceptorLogger(logger), opts...), |
| 98 | + ), |
| 99 | + middleware.WithStreamServerChain( |
| 100 | + tags.StreamServerInterceptor(), |
| 101 | + logging.StreamServerInterceptor(grpcphuslog.InterceptorLogger(logger), opts...), |
| 102 | + ), |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func ExampleServerPayloadLoggingDecider() { |
| 107 | + // Logger is used, allowing pre-definition of certain fields by the user. |
| 108 | + logger := log.DefaultLogger.GrcpGateway() |
| 109 | + // Expect payload from "/blah.foo.healthcheck/Check" call to be logged. |
| 110 | + payloadDecider := func(ctx context.Context, fullMethodName string, servingObject interface{}) bool { |
| 111 | + return fullMethodName == "/blah.foo.healthcheck/Check" |
| 112 | + } |
| 113 | + |
| 114 | + // Create a server, make sure we put the tags context before everything else. |
| 115 | + _ = []grpc.ServerOption{ |
| 116 | + middleware.WithUnaryServerChain( |
| 117 | + tags.UnaryServerInterceptor(), |
| 118 | + logging.UnaryServerInterceptor(grpcphuslog.InterceptorLogger(logger)), |
| 119 | + logging.PayloadUnaryServerInterceptor(grpcphuslog.InterceptorLogger(logger), payloadDecider), |
| 120 | + ), |
| 121 | + middleware.WithStreamServerChain( |
| 122 | + tags.StreamServerInterceptor(), |
| 123 | + logging.StreamServerInterceptor(grpcphuslog.InterceptorLogger(logger)), |
| 124 | + logging.PayloadStreamServerInterceptor(grpcphuslog.InterceptorLogger(logger), payloadDecider), |
| 125 | + ), |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestExamplesBuildable(t *testing.T) { |
| 130 | + Example_initializationWithCustomLevels() |
| 131 | + Example_initializationWithDurationFieldOverride() |
| 132 | + Example_initializationWithCodeGenRequestFieldExtractor() |
| 133 | + ExampleWithDecider() |
| 134 | + ExampleServerPayloadLoggingDecider() |
| 135 | +} |
0 commit comments