Use log severity and verbosity level (#1340)

- All logs use 1 severity level instead of printf
 - All transport logs only go to verbose level 2+
 - The default logger only log errors and verbosity level 1
 - Add environment variable GRPC_GO_LOG_SEVERITY_LEVEL and GRPC_GO_LOG_VERBOSITY_LEVEL to set severity or verbosity levels for the default logger
This commit is contained in:
Menghan Li
2017-07-13 12:10:19 -07:00
committed by GitHub
parent 27b2052c95
commit d6723916d2
12 changed files with 156 additions and 78 deletions

View File

@ -21,6 +21,8 @@
package glogger
import (
"fmt"
"github.com/golang/glog"
"google.golang.org/grpc/grpclog"
)
@ -32,51 +34,51 @@ func init() {
type glogger struct{}
func (g *glogger) Info(args ...interface{}) {
glog.Info(args...)
glog.InfoDepth(2, args...)
}
func (g *glogger) Infoln(args ...interface{}) {
glog.Infoln(args...)
glog.InfoDepth(2, fmt.Sprintln(args...))
}
func (g *glogger) Infof(format string, args ...interface{}) {
glog.Infof(format, args...)
glog.InfoDepth(2, fmt.Sprintf(format, args...))
}
func (g *glogger) Warning(args ...interface{}) {
glog.Warning(args...)
glog.WarningDepth(2, args...)
}
func (g *glogger) Warningln(args ...interface{}) {
glog.Warningln(args...)
glog.WarningDepth(2, fmt.Sprintln(args...))
}
func (g *glogger) Warningf(format string, args ...interface{}) {
glog.Warningf(format, args...)
glog.WarningDepth(2, fmt.Sprintf(format, args...))
}
func (g *glogger) Error(args ...interface{}) {
glog.Error(args...)
glog.ErrorDepth(2, args...)
}
func (g *glogger) Errorln(args ...interface{}) {
glog.Errorln(args...)
glog.ErrorDepth(2, fmt.Sprintln(args...))
}
func (g *glogger) Errorf(format string, args ...interface{}) {
glog.Errorf(format, args...)
glog.ErrorDepth(2, fmt.Sprintf(format, args...))
}
func (g *glogger) Fatal(args ...interface{}) {
glog.Fatal(args...)
glog.FatalDepth(2, args...)
}
func (g *glogger) Fatalln(args ...interface{}) {
glog.Fatalln(args...)
glog.FatalDepth(2, fmt.Sprintln(args...))
}
func (g *glogger) Fatalf(format string, args ...interface{}) {
glog.Fatalf(format, args...)
glog.FatalDepth(2, fmt.Sprintf(format, args...))
}
func (g *glogger) V(l int) bool {

View File

@ -31,7 +31,15 @@
*
*/
package grpclog
// Package grpclog defines logging for grpc.
//
// All logs in transport package only go to verbose level 2.
// All logs in other packages in grpc are logged in spite of the verbosity level.
//
// In the default logger,
// severity level can be set by environment variable GRPC_GO_LOG_SEVERITY_LEVEL,
// verbosity level can be set by GRPC_GO_LOG_VERBOSITY_LEVEL.
package grpclog // import "google.golang.org/grpc/grpclog"
import "os"

View File

@ -16,8 +16,7 @@
*
*/
// Package grpclog defines logging for grpc.
package grpclog // import "google.golang.org/grpc/grpclog"
package grpclog
// Logger mimics golang's standard Logger as an interface.
// Deprecated: use LoggerV2.

View File

@ -31,16 +31,14 @@
*
*/
/*
Package grpclog defines logging for grpc.
*/
package grpclog // import "google.golang.org/grpc/grpclog"
package grpclog
import (
"io"
"io/ioutil"
"log"
"os"
"strconv"
)
// LoggerV2 does underlying logging work for grpclog.
@ -104,6 +102,7 @@ var severityName = []string{
// loggerT is the default logger used by grpclog.
type loggerT struct {
m []*log.Logger
v int
}
// NewLoggerV2 creates a loggerV2 with the provided writers.
@ -112,19 +111,44 @@ type loggerT struct {
// Warning logs will be written to warningW and infoW.
// Info logs will be written to infoW.
func NewLoggerV2(infoW, warningW, errorW io.Writer) LoggerV2 {
return NewLoggerV2WithVerbosity(infoW, warningW, errorW, 0)
}
// NewLoggerV2WithVerbosity creates a loggerV2 with the provided writers and
// verbosity level.
func NewLoggerV2WithVerbosity(infoW, warningW, errorW io.Writer, v int) LoggerV2 {
var m []*log.Logger
m = append(m, log.New(infoW, severityName[infoLog]+": ", log.LstdFlags))
m = append(m, log.New(io.MultiWriter(infoW, warningW), severityName[warningLog]+": ", log.LstdFlags))
ew := io.MultiWriter(infoW, warningW, errorW) // ew will be used for error and fatal.
m = append(m, log.New(ew, severityName[errorLog]+": ", log.LstdFlags))
m = append(m, log.New(ew, severityName[fatalLog]+": ", log.LstdFlags))
return &loggerT{m: m}
return &loggerT{m: m, v: v}
}
// newLoggerV2 creates a loggerV2 to be used as default logger.
// All logs are written to stderr.
func newLoggerV2() LoggerV2 {
return NewLoggerV2(os.Stderr, ioutil.Discard, ioutil.Discard)
errorW := ioutil.Discard
warningW := ioutil.Discard
infoW := ioutil.Discard
logLevel := os.Getenv("GRPC_GO_LOG_SEVERITY_LEVEL")
switch logLevel {
case "", "ERROR", "error": // If env is unset, set level to ERROR.
errorW = os.Stderr
case "WARNING", "warning":
warningW = os.Stderr
case "INFO", "info":
infoW = os.Stderr
}
var v int
vLevel := os.Getenv("GRPC_GO_LOG_VERBOSITY_LEVEL")
if vl, err := strconv.Atoi(vLevel); err == nil {
v = vl
}
return NewLoggerV2WithVerbosity(infoW, warningW, errorW, v)
}
func (g *loggerT) Info(args ...interface{}) {
@ -176,7 +200,5 @@ func (g *loggerT) Fatalf(format string, args ...interface{}) {
}
func (g *loggerT) V(l int) bool {
// Returns true for all verbose level.
// TODO support verbose level in the default logger.
return true
return l <= g.v
}