2020-01-28 23:50:41 +00:00
|
|
|
package logging
|
2018-10-03 19:37:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-01-28 23:50:41 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2018-10-03 19:37:53 +00:00
|
|
|
)
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
// GRPCLogger wrapps a hclog.Logger and implements the grpclog.LoggerV2 interface
|
2018-10-03 19:37:53 +00:00
|
|
|
// allowing gRPC servers to log to the standard Consul logger.
|
|
|
|
type GRPCLogger struct {
|
2020-01-28 23:50:41 +00:00
|
|
|
level string
|
|
|
|
logger hclog.Logger
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGRPCLogger creates a grpclog.LoggerV2 that will output to the supplied
|
|
|
|
// logger with Severity/Verbosity level appropriate for the given config.
|
|
|
|
//
|
|
|
|
// Note that grpclog has Info, Warning, Error, Fatal severity levels AND integer
|
2020-08-20 21:50:00 +00:00
|
|
|
// verbosity levels for additional info. Verbose logs in hclog are always DEBUG
|
2018-10-03 19:37:53 +00:00
|
|
|
// severity so we map Info,V0 to INFO, Info,V1 to DEBUG, and Info,V>1 to TRACE.
|
2020-08-19 15:41:06 +00:00
|
|
|
func NewGRPCLogger(logLevel string, logger hclog.Logger) *GRPCLogger {
|
2018-10-03 19:37:53 +00:00
|
|
|
return &GRPCLogger{
|
2020-08-19 15:41:06 +00:00
|
|
|
level: logLevel,
|
2020-01-28 23:50:41 +00:00
|
|
|
logger: logger,
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info implements grpclog.LoggerV2
|
|
|
|
func (g *GRPCLogger) Info(args ...interface{}) {
|
2020-01-28 23:50:41 +00:00
|
|
|
g.logger.Info(fmt.Sprint(args...))
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Infoln implements grpclog.LoggerV2
|
|
|
|
func (g *GRPCLogger) Infoln(args ...interface{}) {
|
2020-01-28 23:50:41 +00:00
|
|
|
g.Info(fmt.Sprint(args...))
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Infof implements grpclog.LoggerV2
|
|
|
|
func (g *GRPCLogger) Infof(format string, args ...interface{}) {
|
|
|
|
g.Info(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warning implements grpclog.LoggerV2
|
|
|
|
func (g *GRPCLogger) Warning(args ...interface{}) {
|
2020-01-28 23:50:41 +00:00
|
|
|
g.logger.Warn(fmt.Sprint(args...))
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warningln implements grpclog.LoggerV2
|
|
|
|
func (g *GRPCLogger) Warningln(args ...interface{}) {
|
2020-01-28 23:50:41 +00:00
|
|
|
g.Warning(fmt.Sprint(args...))
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warningf implements grpclog.LoggerV2
|
|
|
|
func (g *GRPCLogger) Warningf(format string, args ...interface{}) {
|
|
|
|
g.Warning(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements grpclog.LoggerV2
|
|
|
|
func (g *GRPCLogger) Error(args ...interface{}) {
|
2020-01-28 23:50:41 +00:00
|
|
|
g.logger.Error(fmt.Sprint(args...))
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Errorln implements grpclog.LoggerV2
|
|
|
|
func (g *GRPCLogger) Errorln(args ...interface{}) {
|
2020-01-28 23:50:41 +00:00
|
|
|
g.Error(fmt.Sprint(args...))
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Errorf implements grpclog.LoggerV2
|
|
|
|
func (g *GRPCLogger) Errorf(format string, args ...interface{}) {
|
|
|
|
g.Error(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fatal implements grpclog.LoggerV2
|
|
|
|
func (g *GRPCLogger) Fatal(args ...interface{}) {
|
2020-01-28 23:50:41 +00:00
|
|
|
g.logger.Error(fmt.Sprint(args...))
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fatalln implements grpclog.LoggerV2
|
|
|
|
func (g *GRPCLogger) Fatalln(args ...interface{}) {
|
2020-01-28 23:50:41 +00:00
|
|
|
g.Fatal(fmt.Sprint(args...))
|
2018-10-03 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fatalf implements grpclog.LoggerV2
|
|
|
|
func (g *GRPCLogger) Fatalf(format string, args ...interface{}) {
|
|
|
|
g.Fatal(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// V implements grpclog.LoggerV2
|
|
|
|
func (g *GRPCLogger) V(l int) bool {
|
|
|
|
switch g.level {
|
|
|
|
case "TRACE":
|
|
|
|
// Enable ALL the verbosity!
|
|
|
|
return true
|
|
|
|
case "DEBUG":
|
|
|
|
return l < 2
|
|
|
|
case "INFO":
|
|
|
|
return l < 1
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|