...

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

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

     1  package kit_test
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/go-kit/log"
     7  	grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
     8  	"github.com/grpc-ecosystem/go-grpc-middleware/logging/kit"
     9  	grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
    10  
    11  	"google.golang.org/grpc"
    12  )
    13  
    14  var (
    15  	customFunc kit.CodeToLevel
    16  )
    17  
    18  // Initialization shows a relatively complex initialization sequence.
    19  func Example_initialization() {
    20  	// Logger is used, allowing pre-definition of certain fields by the user.
    21  	logger := log.NewNopLogger()
    22  	// Shared options for the logger, with a custom gRPC code to log level function.
    23  	opts := []kit.Option{
    24  		kit.WithLevels(customFunc),
    25  	}
    26  	// Create a server, make sure we put the grpc_ctxtags context before everything else.
    27  	_ = grpc.NewServer(
    28  		grpc_middleware.WithUnaryServerChain(
    29  			grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
    30  			kit.UnaryServerInterceptor(logger, opts...),
    31  		),
    32  		grpc_middleware.WithStreamServerChain(
    33  			grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
    34  			kit.StreamServerInterceptor(logger, opts...),
    35  		),
    36  	)
    37  }
    38  
    39  func Example_initializationWithDurationFieldOverride() {
    40  	// Logger is used, allowing pre-definition of certain fields by the user.
    41  	logger := log.NewNopLogger()
    42  	// Shared options for the logger, with a custom duration to log field function.
    43  	opts := []kit.Option{
    44  		kit.WithDurationField(func(duration time.Duration) []interface{} {
    45  			return kit.DurationToTimeMillisField(duration)
    46  		}),
    47  	}
    48  	_ = grpc.NewServer(
    49  		grpc_middleware.WithUnaryServerChain(
    50  			grpc_ctxtags.UnaryServerInterceptor(),
    51  			kit.UnaryServerInterceptor(logger, opts...),
    52  		),
    53  		grpc_middleware.WithStreamServerChain(
    54  			grpc_ctxtags.StreamServerInterceptor(),
    55  			kit.StreamServerInterceptor(logger, opts...),
    56  		),
    57  	)
    58  }
    59  
    60  func ExampleWithDecider() {
    61  	opts := []kit.Option{
    62  		kit.WithDecider(func(methodFullName string, err error) bool {
    63  			// will not log gRPC calls if it was a call to healthcheck and no error was raised
    64  			if err == nil && methodFullName == "blah.foo.healthcheck" {
    65  				return false
    66  			}
    67  
    68  			// by default you will log all calls
    69  			return true
    70  		}),
    71  	}
    72  
    73  	_ = []grpc.ServerOption{
    74  		grpc_middleware.WithStreamServerChain(
    75  			grpc_ctxtags.StreamServerInterceptor(),
    76  			kit.StreamServerInterceptor(log.NewNopLogger(), opts...)),
    77  		grpc_middleware.WithUnaryServerChain(
    78  			grpc_ctxtags.UnaryServerInterceptor(),
    79  			kit.UnaryServerInterceptor(log.NewNopLogger(), opts...)),
    80  	}
    81  }
    82  

View as plain text