gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-grpc-middleware/logging/zap/examples_test.go (about)

     1  package grpc_zap_test
     2  
     3  import (
     4  	"context"
     5  	"gitee.com/ks-custle/core-gm/go-grpc-middleware/logging/zap/ctxzap"
     6  	"time"
     7  
     8  	grpcmiddleware "gitee.com/ks-custle/core-gm/go-grpc-middleware"
     9  	grpczap "gitee.com/ks-custle/core-gm/go-grpc-middleware/logging/zap"
    10  	grpcctxtags "gitee.com/ks-custle/core-gm/go-grpc-middleware/tags"
    11  	pbtestproto "gitee.com/ks-custle/core-gm/go-grpc-middleware/testing/testproto"
    12  	"gitee.com/ks-custle/core-gm/grpc"
    13  	"go.uber.org/zap"
    14  	"go.uber.org/zap/zapcore"
    15  )
    16  
    17  var (
    18  	zapLogger  *zap.Logger
    19  	customFunc grpczap.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 := []grpczap.Option{
    26  		grpczap.WithLevels(customFunc),
    27  	}
    28  	// Make sure that log statements internal to gRPC library are logged using the zapLogger as well.
    29  	grpczap.ReplaceGrpcLogger(zapLogger)
    30  	// Create a server, make sure we put the grpc_ctxtags context before everything else.
    31  	_ = grpc.NewServer(
    32  		grpcmiddleware.WithUnaryServerChain(
    33  			grpcctxtags.UnaryServerInterceptor(grpcctxtags.WithFieldExtractor(grpcctxtags.CodeGenRequestFieldExtractor)),
    34  			grpczap.UnaryServerInterceptor(zapLogger, opts...),
    35  		),
    36  		grpcmiddleware.WithStreamServerChain(
    37  			grpcctxtags.StreamServerInterceptor(grpcctxtags.WithFieldExtractor(grpcctxtags.CodeGenRequestFieldExtractor)),
    38  			grpczap.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 := []grpczap.Option{
    46  		grpczap.WithDurationField(func(duration time.Duration) zapcore.Field {
    47  			return zap.Int64("grpc.time_ns", duration.Nanoseconds())
    48  		}),
    49  	}
    50  
    51  	_ = grpc.NewServer(
    52  		grpcmiddleware.WithUnaryServerChain(
    53  			grpcctxtags.UnaryServerInterceptor(),
    54  			grpczap.UnaryServerInterceptor(zapLogger, opts...),
    55  		),
    56  		grpcmiddleware.WithStreamServerChain(
    57  			grpcctxtags.StreamServerInterceptor(),
    58  			grpczap.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 *pbtestproto.PingRequest) (*pbtestproto.PingResponse, error) {
    66  		// Add fields the ctxtags of the request which will be added to all extracted loggers.
    67  		grpcctxtags.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  		// ctx_zap.Extract is deprecated, use ctxzap.Extract instead.
    71  		//l := ctx_zap.Extract(ctx)
    72  		l := ctxzap.Extract(ctx)
    73  		l.Info("some ping")
    74  		l.Info("another ping")
    75  		return &pbtestproto.PingResponse{Value: ping.Value}, nil
    76  	}
    77  }
    78  
    79  func Example_initializationWithDecider() {
    80  	opts := []grpczap.Option{
    81  		grpczap.WithDecider(func(fullMethodName string, err error) bool {
    82  			// will not log gRPC calls if it was a call to healthcheck and no error was raised
    83  			if err == nil && fullMethodName == "foo.bar.healthcheck" {
    84  				return false
    85  			}
    86  
    87  			// by default everything will be logged
    88  			return true
    89  		}),
    90  	}
    91  
    92  	_ = []grpc.ServerOption{
    93  		grpcmiddleware.WithStreamServerChain(
    94  			grpcctxtags.StreamServerInterceptor(),
    95  			grpczap.StreamServerInterceptor(zap.NewNop(), opts...)),
    96  		grpcmiddleware.WithUnaryServerChain(
    97  			grpcctxtags.UnaryServerInterceptor(),
    98  			grpczap.UnaryServerInterceptor(zap.NewNop(), opts...)),
    99  	}
   100  }