...

Source file src/github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/examples_test.go

Documentation: github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus

     1  package grpc_logrus_test
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/grpc-ecosystem/go-grpc-middleware"
     8  	"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
     9  	"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
    10  	"github.com/grpc-ecosystem/go-grpc-middleware/tags"
    11  	pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
    12  	"github.com/sirupsen/logrus"
    13  	"google.golang.org/grpc"
    14  )
    15  
    16  var (
    17  	logrusLogger *logrus.Logger
    18  	customFunc   grpc_logrus.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 := []grpc_logrus.Option{
    27  		grpc_logrus.WithLevels(customFunc),
    28  	}
    29  	// Make sure that log statements internal to gRPC library are logged using the logrus Logger as well.
    30  	grpc_logrus.ReplaceGrpcLogger(logrusEntry)
    31  	// Create a server, make sure we put the grpc_ctxtags context before everything else.
    32  	_ = grpc.NewServer(
    33  		grpc_middleware.WithUnaryServerChain(
    34  			grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
    35  			grpc_logrus.UnaryServerInterceptor(logrusEntry, opts...),
    36  		),
    37  		grpc_middleware.WithStreamServerChain(
    38  			grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
    39  			grpc_logrus.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 := []grpc_logrus.Option{
    49  		grpc_logrus.WithDurationField(func(duration time.Duration) (key string, value interface{}) {
    50  			return "grpc.time_ns", duration.Nanoseconds()
    51  		}),
    52  	}
    53  	_ = grpc.NewServer(
    54  		grpc_middleware.WithUnaryServerChain(
    55  			grpc_ctxtags.UnaryServerInterceptor(),
    56  			grpc_logrus.UnaryServerInterceptor(logrusEntry, opts...),
    57  		),
    58  		grpc_middleware.WithStreamServerChain(
    59  			grpc_ctxtags.StreamServerInterceptor(),
    60  			grpc_logrus.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 *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) {
    68  		// Add fields the ctxtags of the request which will be added to all extracted loggers.
    69  		grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
    70  		// Extract a single request-scoped logrus.Logger and log messages.
    71  		l := ctxlogrus.Extract(ctx)
    72  		l.Info("some ping")
    73  		l.Info("another ping")
    74  		return &pb_testproto.PingResponse{Value: ping.Value}, nil
    75  	}
    76  }
    77  
    78  func ExampleWithDecider() {
    79  	opts := []grpc_logrus.Option{
    80  		grpc_logrus.WithDecider(func(methodFullName string, err error) bool {
    81  			// will not log gRPC calls if it was a call to healthcheck and no error was raised
    82  			if err == nil && methodFullName == "blah.foo.healthcheck" {
    83  				return false
    84  			}
    85  
    86  			// by default you will log all calls
    87  			return true
    88  		}),
    89  	}
    90  
    91  	_ = []grpc.ServerOption{
    92  		grpc_middleware.WithStreamServerChain(
    93  			grpc_ctxtags.StreamServerInterceptor(),
    94  			grpc_logrus.StreamServerInterceptor(logrus.NewEntry(logrus.New()), opts...)),
    95  		grpc_middleware.WithUnaryServerChain(
    96  			grpc_ctxtags.UnaryServerInterceptor(),
    97  			grpc_logrus.UnaryServerInterceptor(logrus.NewEntry(logrus.New()), opts...)),
    98  	}
    99  }
   100  

View as plain text