github.com/koko1123/flow-go-1@v0.29.6/engine/common/rpc/logging_interceptor.go (about)

     1  package rpc
     2  
     3  import (
     4  	grpczerolog "github.com/grpc-ecosystem/go-grpc-middleware/providers/zerolog/v2"
     5  	"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
     6  	"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/tags"
     7  	"github.com/rs/zerolog"
     8  	"google.golang.org/grpc"
     9  	"google.golang.org/grpc/codes"
    10  )
    11  
    12  func customClientCodeToLevel(c codes.Code) logging.Level {
    13  	if c == codes.OK {
    14  		// log successful returns as Debug to avoid excessive logging in info mode
    15  		return logging.DEBUG
    16  	}
    17  	return logging.DefaultServerCodeToLevel(c)
    18  }
    19  
    20  // LoggingInterceptor creates the logging interceptors to log incoming GRPC request and response (minus the payload body)
    21  func LoggingInterceptor(log zerolog.Logger) []grpc.UnaryServerInterceptor {
    22  	tagsInterceptor := tags.UnaryServerInterceptor(tags.WithFieldExtractor(tags.CodeGenRequestFieldExtractor))
    23  	loggingInterceptor := logging.UnaryServerInterceptor(grpczerolog.InterceptorLogger(log), logging.WithLevels(customClientCodeToLevel))
    24  	return []grpc.UnaryServerInterceptor{tagsInterceptor, loggingInterceptor}
    25  }