...

Source file src/github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/grpclogger.go

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

     1  // Copyright 2017 Michal Witkowski. All Rights Reserved.
     2  // See LICENSE for licensing terms.
     3  
     4  package grpc_zap
     5  
     6  import (
     7  	"fmt"
     8  
     9  	grpc_logsettable "github.com/grpc-ecosystem/go-grpc-middleware/logging/settable"
    10  	"go.uber.org/zap"
    11  	"google.golang.org/grpc/grpclog"
    12  )
    13  
    14  // ReplaceGrpcLogger sets the given zap.Logger as a gRPC-level logger.
    15  // This should be called *before* any other initialization, preferably from init() functions.
    16  // Deprecated: use ReplaceGrpcLoggerV2.
    17  func ReplaceGrpcLogger(logger *zap.Logger) {
    18  	zgl := &zapGrpcLogger{logger.With(SystemField, zap.Bool("grpc_log", true))}
    19  	grpclog.SetLogger(zgl)
    20  }
    21  
    22  type zapGrpcLogger struct {
    23  	logger *zap.Logger
    24  }
    25  
    26  func (l *zapGrpcLogger) Fatal(args ...interface{}) {
    27  	l.logger.Fatal(fmt.Sprint(args...))
    28  }
    29  
    30  func (l *zapGrpcLogger) Fatalf(format string, args ...interface{}) {
    31  	l.logger.Fatal(fmt.Sprintf(format, args...))
    32  }
    33  
    34  func (l *zapGrpcLogger) Fatalln(args ...interface{}) {
    35  	l.logger.Fatal(fmt.Sprint(args...))
    36  }
    37  
    38  func (l *zapGrpcLogger) Print(args ...interface{}) {
    39  	l.logger.Info(fmt.Sprint(args...))
    40  }
    41  
    42  func (l *zapGrpcLogger) Printf(format string, args ...interface{}) {
    43  	l.logger.Info(fmt.Sprintf(format, args...))
    44  }
    45  
    46  func (l *zapGrpcLogger) Println(args ...interface{}) {
    47  	l.logger.Info(fmt.Sprint(args...))
    48  }
    49  
    50  // ReplaceGrpcLoggerV2 replaces the grpclog.LoggerV2 with the provided logger.
    51  // It should be called before any gRPC functions. Logging verbosity defaults to info level.
    52  // To adjust gRPC logging verbosity, see ReplaceGrpcLoggerV2WithVerbosity.
    53  func ReplaceGrpcLoggerV2(logger *zap.Logger) {
    54  	ReplaceGrpcLoggerV2WithVerbosity(logger, 0)
    55  }
    56  
    57  // ReplaceGrpcLoggerV2WithVerbosity replaces the grpclog.Logger with the provided logger and verbosity.
    58  // It should be called before any gRPC functions.
    59  // verbosity correlates to grpclogs verbosity levels. A higher verbosity value results in less logging.
    60  func ReplaceGrpcLoggerV2WithVerbosity(logger *zap.Logger, verbosity int) {
    61  	zgl := &zapGrpcLoggerV2{
    62  		logger:    logger.With(SystemField, zap.Bool("grpc_log", true)).WithOptions(zap.AddCallerSkip(2)),
    63  		verbosity: verbosity,
    64  	}
    65  	grpclog.SetLoggerV2(zgl)
    66  }
    67  
    68  // SetGrpcLoggerV2 replaces the grpc_log.Logger with the provided logger.
    69  // It can be used even when grpc infrastructure was initialized.
    70  func SetGrpcLoggerV2(settable grpc_logsettable.SettableLoggerV2, logger *zap.Logger) {
    71  	SetGrpcLoggerV2WithVerbosity(settable, logger, 0)
    72  }
    73  
    74  // SetGrpcLoggerV2WithVerbosity replaces the grpc_.LoggerV2 with the provided logger and verbosity.
    75  // It can be used even when grpc infrastructure was initialized.
    76  func SetGrpcLoggerV2WithVerbosity(settable grpc_logsettable.SettableLoggerV2, logger *zap.Logger, verbosity int) {
    77  	zgl := &zapGrpcLoggerV2{
    78  		logger:    logger.With(SystemField, zap.Bool("grpc_log", true)),
    79  		verbosity: verbosity,
    80  	}
    81  	settable.Set(zgl)
    82  }
    83  
    84  type zapGrpcLoggerV2 struct {
    85  	logger    *zap.Logger
    86  	verbosity int
    87  }
    88  
    89  func (l *zapGrpcLoggerV2) Info(args ...interface{}) {
    90  	l.logger.Info(fmt.Sprint(args...))
    91  }
    92  
    93  func (l *zapGrpcLoggerV2) Infoln(args ...interface{}) {
    94  	l.logger.Info(fmt.Sprint(args...))
    95  }
    96  
    97  func (l *zapGrpcLoggerV2) Infof(format string, args ...interface{}) {
    98  	l.logger.Info(fmt.Sprintf(format, args...))
    99  }
   100  
   101  func (l *zapGrpcLoggerV2) Warning(args ...interface{}) {
   102  	l.logger.Warn(fmt.Sprint(args...))
   103  }
   104  
   105  func (l *zapGrpcLoggerV2) Warningln(args ...interface{}) {
   106  	l.logger.Warn(fmt.Sprint(args...))
   107  }
   108  
   109  func (l *zapGrpcLoggerV2) Warningf(format string, args ...interface{}) {
   110  	l.logger.Warn(fmt.Sprintf(format, args...))
   111  }
   112  
   113  func (l *zapGrpcLoggerV2) Error(args ...interface{}) {
   114  	l.logger.Error(fmt.Sprint(args...))
   115  }
   116  
   117  func (l *zapGrpcLoggerV2) Errorln(args ...interface{}) {
   118  	l.logger.Error(fmt.Sprint(args...))
   119  }
   120  
   121  func (l *zapGrpcLoggerV2) Errorf(format string, args ...interface{}) {
   122  	l.logger.Error(fmt.Sprintf(format, args...))
   123  }
   124  
   125  func (l *zapGrpcLoggerV2) Fatal(args ...interface{}) {
   126  	l.logger.Fatal(fmt.Sprint(args...))
   127  }
   128  
   129  func (l *zapGrpcLoggerV2) Fatalln(args ...interface{}) {
   130  	l.logger.Fatal(fmt.Sprint(args...))
   131  }
   132  
   133  func (l *zapGrpcLoggerV2) Fatalf(format string, args ...interface{}) {
   134  	l.logger.Fatal(fmt.Sprintf(format, args...))
   135  }
   136  
   137  func (l *zapGrpcLoggerV2) V(level int) bool {
   138  	// Check whether the verbosity of the current log ('level') is within the specified threshold ('l.verbosity').
   139  	// As in https://github.com/grpc/grpc-go/blob/41e044e1c82fcf6a5801d6cbd7ecf952505eecb1/grpclog/loggerv2.go#L199-L201.
   140  	return level <= l.verbosity
   141  }
   142  

View as plain text