gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-grpc-middleware/logging/logrus/examples_test.go (about) 1 package grpc_logrus_test 2 3 import ( 4 "gitee.com/ks-custle/core-gm/go-grpc-middleware/logging/logrus/ctxlogrus" 5 "time" 6 7 grpcmiddleware "gitee.com/ks-custle/core-gm/go-grpc-middleware" 8 grpclogrus "gitee.com/ks-custle/core-gm/go-grpc-middleware/logging/logrus" 9 grpcctxtags "gitee.com/ks-custle/core-gm/go-grpc-middleware/tags" 10 pbtestproto "gitee.com/ks-custle/core-gm/go-grpc-middleware/testing/testproto" 11 "gitee.com/ks-custle/core-gm/grpc" 12 "gitee.com/ks-custle/core-gm/net/context" 13 "github.com/sirupsen/logrus" 14 ) 15 16 var ( 17 logrusLogger *logrus.Logger 18 customFunc grpclogrus.CodeToLevel 19 ) 20 21 // Initialization shows a relatively complex initialization sequence. 22 func Example_initialization() { 23 // Logrus entry is used, allowing pre-definition of certain fields by the user. 24 logrusEntry := logrus.NewEntry(logrusLogger) 25 // Shared options for the logger, with a custom gRPC code to log level function. 26 opts := []grpclogrus.Option{ 27 grpclogrus.WithLevels(customFunc), 28 } 29 // Make sure that log statements internal to gRPC library are logged using the logrus Logger as well. 30 grpclogrus.ReplaceGrpcLogger(logrusEntry) 31 // Create a server, make sure we put the grpc_ctxtags context before everything else. 32 _ = grpc.NewServer( 33 grpcmiddleware.WithUnaryServerChain( 34 grpcctxtags.UnaryServerInterceptor(grpcctxtags.WithFieldExtractor(grpcctxtags.CodeGenRequestFieldExtractor)), 35 grpclogrus.UnaryServerInterceptor(logrusEntry, opts...), 36 ), 37 grpcmiddleware.WithStreamServerChain( 38 grpcctxtags.StreamServerInterceptor(grpcctxtags.WithFieldExtractor(grpcctxtags.CodeGenRequestFieldExtractor)), 39 grpclogrus.StreamServerInterceptor(logrusEntry, opts...), 40 ), 41 ) 42 } 43 44 func Example_initializationWithDurationFieldOverride() { 45 // Logrus entry is used, allowing pre-definition of certain fields by the user. 46 logrusEntry := logrus.NewEntry(logrusLogger) 47 // Shared options for the logger, with a custom duration to log field function. 48 opts := []grpclogrus.Option{ 49 grpclogrus.WithDurationField(func(duration time.Duration) (key string, value interface{}) { 50 return "grpc.time_ns", duration.Nanoseconds() 51 }), 52 } 53 _ = grpc.NewServer( 54 grpcmiddleware.WithUnaryServerChain( 55 grpcctxtags.UnaryServerInterceptor(), 56 grpclogrus.UnaryServerInterceptor(logrusEntry, opts...), 57 ), 58 grpcmiddleware.WithStreamServerChain( 59 grpcctxtags.StreamServerInterceptor(), 60 grpclogrus.StreamServerInterceptor(logrusEntry, opts...), 61 ), 62 ) 63 } 64 65 // Simple unary handler that adds custom fields to the requests's context. These will be used for all log statements. 66 func ExampleExtract_unary() { 67 _ = func(ctx context.Context, ping *pbtestproto.PingRequest) (*pbtestproto.PingResponse, error) { 68 // Add fields the ctxtags of the request which will be added to all extracted loggers. 69 grpcctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337) 70 // Extract a single request-scoped logrus.Logger and log messages. 71 // ctx_logrus.Extract is deprecated, use the ctxlogrus.Extract instead. 72 //l := ctx_logrus.Extract(ctx) 73 l := ctxlogrus.Extract(ctx) 74 l.Info("some ping") 75 l.Info("another ping") 76 return &pbtestproto.PingResponse{Value: ping.Value}, nil 77 } 78 } 79 80 func ExampleWithDecider() { 81 opts := []grpclogrus.Option{ 82 grpclogrus.WithDecider(func(methodFullName string, err error) bool { 83 // will not log gRPC calls if it was a call to healthcheck and no error was raised 84 if err == nil && methodFullName == "blah.foo.healthcheck" { 85 return false 86 } 87 88 // by default you will log all calls 89 return true 90 }), 91 } 92 93 _ = []grpc.ServerOption{ 94 grpcmiddleware.WithStreamServerChain( 95 grpcctxtags.StreamServerInterceptor(), 96 grpclogrus.StreamServerInterceptor(logrus.NewEntry(logrus.New()), opts...)), 97 grpcmiddleware.WithUnaryServerChain( 98 grpcctxtags.UnaryServerInterceptor(), 99 grpclogrus.UnaryServerInterceptor(logrus.NewEntry(logrus.New()), opts...)), 100 } 101 }