github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-grpc-middleware/logging/zap/examples_test.go (about) 1 package grpc_zap_test 2 3 import ( 4 "context" 5 "time" 6 7 grpc_middleware "github.com/hxx258456/ccgo/go-grpc-middleware" 8 grpc_zap "github.com/hxx258456/ccgo/go-grpc-middleware/logging/zap" 9 grpc_ctxtags "github.com/hxx258456/ccgo/go-grpc-middleware/tags" 10 ctx_zap "github.com/hxx258456/ccgo/go-grpc-middleware/tags/zap" 11 pb_testproto "github.com/hxx258456/ccgo/go-grpc-middleware/testing/testproto" 12 "github.com/hxx258456/ccgo/grpc" 13 "go.uber.org/zap" 14 "go.uber.org/zap/zapcore" 15 ) 16 17 var ( 18 zapLogger *zap.Logger 19 customFunc grpc_zap.CodeToLevel 20 ) 21 22 // Initialization shows a relatively complex initialization sequence. 23 func Example_initialization() { 24 // Shared options for the logger, with a custom gRPC code to log level function. 25 opts := []grpc_zap.Option{ 26 grpc_zap.WithLevels(customFunc), 27 } 28 // Make sure that log statements internal to gRPC library are logged using the zapLogger as well. 29 grpc_zap.ReplaceGrpcLogger(zapLogger) 30 // Create a server, make sure we put the grpc_ctxtags context before everything else. 31 _ = grpc.NewServer( 32 grpc_middleware.WithUnaryServerChain( 33 grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)), 34 grpc_zap.UnaryServerInterceptor(zapLogger, opts...), 35 ), 36 grpc_middleware.WithStreamServerChain( 37 grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)), 38 grpc_zap.StreamServerInterceptor(zapLogger, opts...), 39 ), 40 ) 41 } 42 43 // Initialization shows an initialization sequence with the duration field generation overridden. 44 func Example_initializationWithDurationFieldOverride() { 45 opts := []grpc_zap.Option{ 46 grpc_zap.WithDurationField(func(duration time.Duration) zapcore.Field { 47 return zap.Int64("grpc.time_ns", duration.Nanoseconds()) 48 }), 49 } 50 51 _ = grpc.NewServer( 52 grpc_middleware.WithUnaryServerChain( 53 grpc_ctxtags.UnaryServerInterceptor(), 54 grpc_zap.UnaryServerInterceptor(zapLogger, opts...), 55 ), 56 grpc_middleware.WithStreamServerChain( 57 grpc_ctxtags.StreamServerInterceptor(), 58 grpc_zap.StreamServerInterceptor(zapLogger, opts...), 59 ), 60 ) 61 } 62 63 // Simple unary handler that adds custom fields to the requests's context. These will be used for all log statements. 64 func ExampleExtract_unary() { 65 _ = func(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) { 66 // Add fields the ctxtags of the request which will be added to all extracted loggers. 67 grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337) 68 69 // Extract a single request-scoped zap.Logger and log messages. (containing the grpc.xxx tags) 70 l := ctx_zap.Extract(ctx) 71 l.Info("some ping") 72 l.Info("another ping") 73 return &pb_testproto.PingResponse{Value: ping.Value}, nil 74 } 75 } 76 77 func Example_initializationWithDecider() { 78 opts := []grpc_zap.Option{ 79 grpc_zap.WithDecider(func(fullMethodName string, err error) bool { 80 // will not log gRPC calls if it was a call to healthcheck and no error was raised 81 if err == nil && fullMethodName == "foo.bar.healthcheck" { 82 return false 83 } 84 85 // by default everything will be logged 86 return true 87 }), 88 } 89 90 _ = []grpc.ServerOption{ 91 grpc_middleware.WithStreamServerChain( 92 grpc_ctxtags.StreamServerInterceptor(), 93 grpc_zap.StreamServerInterceptor(zap.NewNop(), opts...)), 94 grpc_middleware.WithUnaryServerChain( 95 grpc_ctxtags.UnaryServerInterceptor(), 96 grpc_zap.UnaryServerInterceptor(zap.NewNop(), opts...)), 97 } 98 }