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

     1  package log
     2  
     3  // GrpcGatewayLogger implements methods to satisfy interface
     4  // github.com/grpc-ecosystem/go-grpc-middleware/blob/v2/interceptors/logging/logging.go
     5  type GrpcGatewayLogger struct {
     6  	logger  Logger
     7  	context Context
     8  }
     9  
    10  // GrpcGateway wraps the Logger to provide a GrpcGateway logger
    11  func (l *Logger) GrpcGateway() GrpcGatewayLogger {
    12  	return GrpcGatewayLogger{
    13  		logger: *l,
    14  	}
    15  }
    16  
    17  // WithValues adds some key-value pairs of context to a logger.
    18  // See Info for documentation on how key/value pairs work.
    19  func (g GrpcGatewayLogger) WithValues(keysAndValues ...interface{}) GrpcGatewayLogger {
    20  	g.context = append(g.context, NewContext(nil).KeysAndValues(keysAndValues...).Value()...)
    21  	return g
    22  }
    23  
    24  // Debug logs a debug with the message and key/value pairs as context.
    25  func (g GrpcGatewayLogger) Debug(msg string) {
    26  	if g.logger.silent(DebugLevel) {
    27  		return
    28  	}
    29  	e := g.logger.header(DebugLevel)
    30  	if caller, full := g.logger.Caller, false; caller != 0 {
    31  		if caller < 0 {
    32  			caller, full = -caller, true
    33  		}
    34  		var rpc [1]uintptr
    35  		e.caller(callers(caller, rpc[:]), rpc[:], full)
    36  	}
    37  	e.Context(g.context).Msg(msg)
    38  }
    39  
    40  // Info logs an info with the message and key/value pairs as context.
    41  func (g GrpcGatewayLogger) Info(msg string) {
    42  	if g.logger.silent(InfoLevel) {
    43  		return
    44  	}
    45  	e := g.logger.header(InfoLevel)
    46  	if caller, full := g.logger.Caller, false; caller != 0 {
    47  		if caller < 0 {
    48  			caller, full = -caller, true
    49  		}
    50  		var rpc [1]uintptr
    51  		e.caller(callers(caller, rpc[:]), rpc[:], full)
    52  	}
    53  	e.Context(g.context).Msg(msg)
    54  }
    55  
    56  // Warning logs a warning with the message and key/value pairs as context.
    57  func (g GrpcGatewayLogger) Warning(msg string) {
    58  	if g.logger.silent(WarnLevel) {
    59  		return
    60  	}
    61  	e := g.logger.header(WarnLevel)
    62  	if caller, full := g.logger.Caller, false; caller != 0 {
    63  		if caller < 0 {
    64  			caller, full = -caller, true
    65  		}
    66  		var rpc [1]uintptr
    67  		e.caller(callers(caller, rpc[:]), rpc[:], full)
    68  	}
    69  	e.Context(g.context).Msg(msg)
    70  }
    71  
    72  // Error logs an error with the message and key/value pairs as context.
    73  func (g GrpcGatewayLogger) Error(msg string) {
    74  	if g.logger.silent(ErrorLevel) {
    75  		return
    76  	}
    77  	e := g.logger.header(ErrorLevel)
    78  	if caller, full := g.logger.Caller, false; caller != 0 {
    79  		if caller < 0 {
    80  			caller, full = -caller, true
    81  		}
    82  		var rpc [1]uintptr
    83  		e.caller(callers(caller, rpc[:]), rpc[:], full)
    84  	}
    85  	e.Context(g.context).Msg(msg)
    86  }