github.com/lmb/consul@v1.4.1/logger/grpc.go (about)

     1  package logger
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  )
     7  
     8  // GRPCLogger wrapps a *log.Logger and implements the grpclog.LoggerV2 interface
     9  // allowing gRPC servers to log to the standard Consul logger.
    10  type GRPCLogger struct {
    11  	level string
    12  	l     *log.Logger
    13  }
    14  
    15  // NewGRPCLogger creates a grpclog.LoggerV2 that will output to the supplied
    16  // logger with Severity/Verbosity level appropriate for the given config.
    17  //
    18  // Note that grpclog has Info, Warning, Error, Fatal severity levels AND integer
    19  // verbosity levels for additional info. Verbose logs in glog are always INFO
    20  // severity so we map Info,V0 to INFO, Info,V1 to DEBUG, and Info,V>1 to TRACE.
    21  func NewGRPCLogger(config *Config, logger *log.Logger) *GRPCLogger {
    22  	return &GRPCLogger{
    23  		level: config.LogLevel,
    24  		l:     logger,
    25  	}
    26  }
    27  
    28  // Info implements grpclog.LoggerV2
    29  func (g *GRPCLogger) Info(args ...interface{}) {
    30  	args = append([]interface{}{"[INFO] "}, args...)
    31  	g.l.Print(args...)
    32  }
    33  
    34  // Infoln implements grpclog.LoggerV2
    35  func (g *GRPCLogger) Infoln(args ...interface{}) {
    36  	g.Info(fmt.Sprintln(args...))
    37  }
    38  
    39  // Infof implements grpclog.LoggerV2
    40  func (g *GRPCLogger) Infof(format string, args ...interface{}) {
    41  	g.Info(fmt.Sprintf(format, args...))
    42  }
    43  
    44  // Warning implements grpclog.LoggerV2
    45  func (g *GRPCLogger) Warning(args ...interface{}) {
    46  	args = append([]interface{}{"[WARN] "}, args...)
    47  	g.l.Print(args...)
    48  }
    49  
    50  // Warningln implements grpclog.LoggerV2
    51  func (g *GRPCLogger) Warningln(args ...interface{}) {
    52  	g.Warning(fmt.Sprintln(args...))
    53  }
    54  
    55  // Warningf implements grpclog.LoggerV2
    56  func (g *GRPCLogger) Warningf(format string, args ...interface{}) {
    57  	g.Warning(fmt.Sprintf(format, args...))
    58  }
    59  
    60  // Error implements grpclog.LoggerV2
    61  func (g *GRPCLogger) Error(args ...interface{}) {
    62  	args = append([]interface{}{"[ERR] "}, args...)
    63  	g.l.Print(args...)
    64  }
    65  
    66  // Errorln implements grpclog.LoggerV2
    67  func (g *GRPCLogger) Errorln(args ...interface{}) {
    68  	g.Error(fmt.Sprintln(args...))
    69  }
    70  
    71  // Errorf implements grpclog.LoggerV2
    72  func (g *GRPCLogger) Errorf(format string, args ...interface{}) {
    73  	g.Error(fmt.Sprintf(format, args...))
    74  }
    75  
    76  // Fatal implements grpclog.LoggerV2
    77  func (g *GRPCLogger) Fatal(args ...interface{}) {
    78  	args = append([]interface{}{"[ERR] "}, args...)
    79  	g.l.Fatal(args...)
    80  }
    81  
    82  // Fatalln implements grpclog.LoggerV2
    83  func (g *GRPCLogger) Fatalln(args ...interface{}) {
    84  	g.Fatal(fmt.Sprintln(args...))
    85  }
    86  
    87  // Fatalf implements grpclog.LoggerV2
    88  func (g *GRPCLogger) Fatalf(format string, args ...interface{}) {
    89  	g.Fatal(fmt.Sprintf(format, args...))
    90  }
    91  
    92  // V implements grpclog.LoggerV2
    93  func (g *GRPCLogger) V(l int) bool {
    94  	switch g.level {
    95  	case "TRACE":
    96  		// Enable ALL the verbosity!
    97  		return true
    98  	case "DEBUG":
    99  		return l < 2
   100  	case "INFO":
   101  		return l < 1
   102  	default:
   103  		return false
   104  	}
   105  }