github.com/sujit-baniya/log@v1.0.73/grpc.go (about)

     1  package log
     2  
     3  // GrpcLogger implements methods to satisfy interface
     4  // google.golang.org/grpc/grpclog.LoggerV2.
     5  type GrpcLogger struct {
     6  	logger  Logger
     7  	context Context
     8  }
     9  
    10  // Grpc wraps the Logger to provide a LoggerV2 logger
    11  func (l *Logger) Grpc(context Context) (g *GrpcLogger) {
    12  	g = &GrpcLogger{
    13  		logger:  *l,
    14  		context: context,
    15  	}
    16  	return
    17  }
    18  
    19  // Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
    20  func (g *GrpcLogger) Info(args ...interface{}) {
    21  	if g.logger.silent(InfoLevel) {
    22  		return
    23  	}
    24  	e := g.logger.header(InfoLevel)
    25  	if caller, full := g.logger.Caller, false; caller != 0 {
    26  		if caller < 0 {
    27  			caller, full = -caller, true
    28  		}
    29  		var rpc [1]uintptr
    30  		e.caller(callers(caller, rpc[:]), rpc[:], full)
    31  	}
    32  	e.Context(g.context).Msgs(args...)
    33  }
    34  
    35  // Infoln logs to INFO log. Arguments are handled in the manner of fmt.Println.
    36  func (g *GrpcLogger) Infoln(args ...interface{}) {
    37  	if g.logger.silent(InfoLevel) {
    38  		return
    39  	}
    40  	e := g.logger.header(InfoLevel)
    41  	if caller, full := g.logger.Caller, false; caller != 0 {
    42  		if caller < 0 {
    43  			caller, full = -caller, true
    44  		}
    45  		var rpc [1]uintptr
    46  		e.caller(callers(caller, rpc[:]), rpc[:], full)
    47  	}
    48  	e.Context(g.context).Msgs(args...)
    49  }
    50  
    51  // Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
    52  func (g *GrpcLogger) Infof(format string, args ...interface{}) {
    53  	if g.logger.silent(InfoLevel) {
    54  		return
    55  	}
    56  	e := g.logger.header(InfoLevel)
    57  	if caller, full := g.logger.Caller, false; caller != 0 {
    58  		if caller < 0 {
    59  			caller, full = -caller, true
    60  		}
    61  		var rpc [1]uintptr
    62  		e.caller(callers(caller, rpc[:]), rpc[:], full)
    63  	}
    64  	e.Context(g.context).Msgf(format, args...)
    65  }
    66  
    67  // Warning logs to WARNING log. Arguments are handled in the manner of fmt.Print.
    68  func (g *GrpcLogger) Warning(args ...interface{}) {
    69  	if g.logger.silent(WarnLevel) {
    70  		return
    71  	}
    72  	e := g.logger.header(WarnLevel)
    73  	if caller, full := g.logger.Caller, false; caller != 0 {
    74  		if caller < 0 {
    75  			caller, full = -caller, true
    76  		}
    77  		var rpc [1]uintptr
    78  		e.caller(callers(caller, rpc[:]), rpc[:], full)
    79  	}
    80  	e.Context(g.context).Msgs(args...)
    81  }
    82  
    83  // Warningln logs to WARNING log. Arguments are handled in the manner of fmt.Println.
    84  func (g *GrpcLogger) Warningln(args ...interface{}) {
    85  	if g.logger.silent(WarnLevel) {
    86  		return
    87  	}
    88  	e := g.logger.header(WarnLevel)
    89  	if caller, full := g.logger.Caller, false; caller != 0 {
    90  		if caller < 0 {
    91  			caller, full = -caller, true
    92  		}
    93  		var rpc [1]uintptr
    94  		e.caller(callers(caller, rpc[:]), rpc[:], full)
    95  	}
    96  	e.Context(g.context).Msgs(args...)
    97  }
    98  
    99  // Warningf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
   100  func (g *GrpcLogger) Warningf(format string, args ...interface{}) {
   101  	if g.logger.silent(WarnLevel) {
   102  		return
   103  	}
   104  	e := g.logger.header(WarnLevel)
   105  	if caller, full := g.logger.Caller, false; caller != 0 {
   106  		if caller < 0 {
   107  			caller, full = -caller, true
   108  		}
   109  		var rpc [1]uintptr
   110  		e.caller(callers(caller, rpc[:]), rpc[:], full)
   111  	}
   112  	e.Context(g.context).Msgf(format, args...)
   113  }
   114  
   115  // Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
   116  func (g *GrpcLogger) Error(args ...interface{}) {
   117  	if g.logger.silent(ErrorLevel) {
   118  		return
   119  	}
   120  	e := g.logger.header(ErrorLevel)
   121  	if caller, full := g.logger.Caller, false; caller != 0 {
   122  		if caller < 0 {
   123  			caller, full = -caller, true
   124  		}
   125  		var rpc [1]uintptr
   126  		e.caller(callers(caller, rpc[:]), rpc[:], full)
   127  	}
   128  	e.Context(g.context).Msgs(args...)
   129  }
   130  
   131  // Errorln logs to ERROR log. Arguments are handled in the manner of fmt.Println.
   132  func (g *GrpcLogger) Errorln(args ...interface{}) {
   133  	if g.logger.silent(ErrorLevel) {
   134  		return
   135  	}
   136  	e := g.logger.header(ErrorLevel)
   137  	if caller, full := g.logger.Caller, false; caller != 0 {
   138  		if caller < 0 {
   139  			caller, full = -caller, true
   140  		}
   141  		var rpc [1]uintptr
   142  		e.caller(callers(caller, rpc[:]), rpc[:], full)
   143  	}
   144  	e.Context(g.context).Msgs(args...)
   145  }
   146  
   147  // Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
   148  func (g *GrpcLogger) Errorf(format string, args ...interface{}) {
   149  	if g.logger.silent(ErrorLevel) {
   150  		return
   151  	}
   152  	e := g.logger.header(ErrorLevel)
   153  	if caller, full := g.logger.Caller, false; caller != 0 {
   154  		if caller < 0 {
   155  			caller, full = -caller, true
   156  		}
   157  		var rpc [1]uintptr
   158  		e.caller(callers(caller, rpc[:]), rpc[:], full)
   159  	}
   160  	e.Context(g.context).Msgf(format, args...)
   161  }
   162  
   163  // Fatal logs to ERROR log. Arguments are handled in the manner of fmt.Print.
   164  // gRPC ensures that all Fatal logs will exit with os.Exit(1).
   165  // Implementations may also call os.Exit() with a non-zero exit code.
   166  func (g *GrpcLogger) Fatal(args ...interface{}) {
   167  	if g.logger.silent(FatalLevel) {
   168  		return
   169  	}
   170  	e := g.logger.header(FatalLevel)
   171  	if caller, full := g.logger.Caller, false; caller != 0 {
   172  		if caller < 0 {
   173  			caller, full = -caller, true
   174  		}
   175  		var rpc [1]uintptr
   176  		e.caller(callers(caller, rpc[:]), rpc[:], full)
   177  	}
   178  	e.Context(g.context).Msgs(args...)
   179  }
   180  
   181  // Fatalln logs to ERROR log. Arguments are handled in the manner of fmt.Println.
   182  // gRPC ensures that all Fatal logs will exit with os.Exit(1).
   183  // Implementations may also call os.Exit() with a non-zero exit code.
   184  func (g *GrpcLogger) Fatalln(args ...interface{}) {
   185  	if g.logger.silent(FatalLevel) {
   186  		return
   187  	}
   188  	e := g.logger.header(FatalLevel)
   189  	if caller, full := g.logger.Caller, false; caller != 0 {
   190  		if caller < 0 {
   191  			caller, full = -caller, true
   192  		}
   193  		var rpc [1]uintptr
   194  		e.caller(callers(caller, rpc[:]), rpc[:], full)
   195  	}
   196  	e.Context(g.context).Msgs(args...)
   197  }
   198  
   199  // Fatalf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
   200  // gRPC ensures that all Fatal logs will exit with os.Exit(1).
   201  // Implementations may also call os.Exit() with a non-zero exit code.
   202  func (g *GrpcLogger) Fatalf(format string, args ...interface{}) {
   203  	if g.logger.silent(FatalLevel) {
   204  		return
   205  	}
   206  	e := g.logger.header(FatalLevel)
   207  	if caller, full := g.logger.Caller, false; caller != 0 {
   208  		if caller < 0 {
   209  			caller, full = -caller, true
   210  		}
   211  		var rpc [1]uintptr
   212  		e.caller(callers(caller, rpc[:]), rpc[:], full)
   213  	}
   214  	e.Context(g.context).Msgf(format, args...)
   215  }
   216  
   217  // V reports whether verbosity level l is at least the requested verbose leveg.
   218  func (g *GrpcLogger) V(level int) bool {
   219  	return level >= int(g.logger.Level)
   220  }
   221  
   222  type grpcLoggerV2 interface {
   223  	Info(args ...interface{})
   224  	Infoln(args ...interface{})
   225  	Infof(format string, args ...interface{})
   226  	Warning(args ...interface{})
   227  	Warningln(args ...interface{})
   228  	Warningf(format string, args ...interface{})
   229  	Error(args ...interface{})
   230  	Errorln(args ...interface{})
   231  	Errorf(format string, args ...interface{})
   232  	Fatal(args ...interface{})
   233  	Fatalln(args ...interface{})
   234  	Fatalf(format string, args ...interface{})
   235  	V(l int) bool
   236  }
   237  
   238  var _ grpcLoggerV2 = (*GrpcLogger)(nil)