Skip to content

Commit

Permalink
change logger API to include all functions
Browse files Browse the repository at this point in the history
  • Loading branch information
menghanl committed Mar 15, 2017
1 parent acc7eee commit 96ddbb9
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 111 deletions.
77 changes: 42 additions & 35 deletions grpclog/glogger/glogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ Package glogger defines glog-based logging for grpc.
package glogger

import (
"fmt"

"github.com/golang/glog"
"google.golang.org/grpc/grpclog"
)
Expand All @@ -49,43 +47,52 @@ func init() {

type glogger struct{}

func (g *glogger) Print(l grpclog.Severity, args ...interface{}) {
switch l {
case grpclog.InfoLog:
glog.InfoDepth(2, args...)
case grpclog.WarningLog:
glog.WarningDepth(2, args...)
case grpclog.ErrorLog:
glog.ErrorDepth(2, args...)
case grpclog.FatalLog:
glog.FatalDepth(2, args...)
}
func (g *glogger) Info(args ...interface{}) {
glog.Info(args...)
}

func (g *glogger) Infoln(args ...interface{}) {
glog.Infoln(args...)
}

func (g *glogger) Infof(format string, args ...interface{}) {
glog.Infof(format, args...)
}

func (g *glogger) Warning(args ...interface{}) {
glog.Warning(args...)
}

func (g *glogger) Warningln(args ...interface{}) {
glog.Warningln(args...)
}

func (g *glogger) Warningf(format string, args ...interface{}) {
glog.Warningf(format, args...)
}

func (g *glogger) Error(args ...interface{}) {
glog.Error(args...)
}

func (g *glogger) Errorln(args ...interface{}) {
glog.Errorln(args...)
}

func (g *glogger) Errorf(format string, args ...interface{}) {
glog.Errorf(format, args...)
}

func (g *glogger) Fatal(args ...interface{}) {
glog.Fatal(args...)
}

func (g *glogger) Println(l grpclog.Severity, args ...interface{}) {
switch l {
case grpclog.InfoLog:
glog.InfoDepth(2, fmt.Sprintln(args...))
case grpclog.WarningLog:
glog.WarningDepth(2, fmt.Sprintln(args...))
case grpclog.ErrorLog:
glog.ErrorDepth(2, fmt.Sprintln(args...))
case grpclog.FatalLog:
glog.FatalDepth(2, fmt.Sprintln(args...))
}
func (g *glogger) Fatalln(args ...interface{}) {
glog.Fatalln(args...)
}

func (g *glogger) Printf(l grpclog.Severity, format string, args ...interface{}) {
switch l {
case grpclog.InfoLog:
glog.InfoDepth(2, fmt.Sprintf(format, args...))
case grpclog.WarningLog:
glog.WarningDepth(2, fmt.Sprintf(format, args...))
case grpclog.ErrorLog:
glog.ErrorDepth(2, fmt.Sprintf(format, args...))
case grpclog.FatalLog:
glog.FatalDepth(2, fmt.Sprintf(format, args...))
}
func (g *glogger) Fatalf(format string, args ...interface{}) {
glog.Fatalf(format, args...)
}

func (g *glogger) V(l grpclog.VerboseLevel) bool {
Expand Down
175 changes: 106 additions & 69 deletions grpclog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,38 @@ import (
"os"
)

// Severity identifies the log severity: info, error, warning, fatal.
type Severity int32

const (
// InfoLog indicates Info severity.
InfoLog Severity = iota
// WarningLog indicates Warning severity.
WarningLog
// ErrorLog indicates Error severity.
ErrorLog
// FatalLog indicates Fatal severity.
// Print() with FatalLog should call os.Exit() with a non-zero exit code.
FatalLog
)

// SeverityName contains the string representation of each severity.
var SeverityName = []string{
InfoLog: "INFO",
WarningLog: "WARNING",
ErrorLog: "ERROR",
FatalLog: "FATAL",
}

// VerboseLevel identifies the verbose level used in grpclog.V() function.
type VerboseLevel int32

// Logger does underlying logging work for grpclog.
type Logger interface {
// Print logs args. Arguments are handled in the manner of fmt.Print.
// l specifies the logging severity for this Print().
Print(l Severity, args ...interface{})
// Println logs args. Arguments are handled in the manner of fmt.Println.
Println(l Severity, args ...interface{})
// Printf logs args. Arguments are handled in the manner of fmt.Printf.
Printf(l Severity, format string, args ...interface{})
// Info logs args. Arguments are handled in the manner of fmt.Print.
Info(args ...interface{})
// Infoln logs args. Arguments are handled in the manner of fmt.Println.
Infoln(args ...interface{})
// Infof logs args. Arguments are handled in the manner of fmt.Printf.
Infof(format string, args ...interface{})
// Warning logs args. Arguments are handled in the manner of fmt.Print.
Warning(args ...interface{})
// Warningln logs args. Arguments are handled in the manner of fmt.Println.
Warningln(args ...interface{})
// Warningf logs args. Arguments are handled in the manner of fmt.Printf.
Warningf(format string, args ...interface{})
// Error logs args. Arguments are handled in the manner of fmt.Print.
Error(args ...interface{})
// Errorln logs args. Arguments are handled in the manner of fmt.Println.
Errorln(args ...interface{})
// Errorf logs args. Arguments are handled in the manner of fmt.Printf.
Errorf(format string, args ...interface{})
// Fatal logs args. Arguments are handled in the manner of fmt.Print.
// This function should call os.Exit() with a non-zero exit code.
Fatal(args ...interface{})
// Fatalln logs args. Arguments are handled in the manner of fmt.Println.
// This function should call os.Exit() with a non-zero exit code.
Fatalln(args ...interface{})
// Fatalf logs args. Arguments are handled in the manner of fmt.Printf.
// This function should call os.Exit() with a non-zero exit code.
Fatalf(format string, args ...interface{})
// V reports whether verbosity level l is at least the requested verbose level.
V(l VerboseLevel) bool
}
Expand All @@ -86,6 +83,25 @@ func SetLogger(l Logger) {
logger = l
}

const (
// infoLog indicates Info severity.
infoLog int = iota
// warningLog indicates Warning severity.
warningLog
// errorLog indicates Error severity.
errorLog
// fatalLog indicates Fatal severity.
fatalLog
)

// severityName contains the string representation of each severity.
var severityName = []string{
infoLog: "INFO",
warningLog: "WARNING",
errorLog: "ERROR",
fatalLog: "FATAL",
}

// loggerT is the default logger used by grpclog.
type loggerT struct {
m []*log.Logger
Expand All @@ -94,41 +110,62 @@ type loggerT struct {
// newLogger creates a default logger.
func newLogger() Logger {
var m []*log.Logger
for s := range SeverityName {
if s == int(FatalLog) {
for s := range severityName {
if s == int(fatalLog) {
// Don't create logger for FatalLog, use InfoLog instead.
break
}
m = append(m, log.New(os.Stderr, SeverityName[s]+": ", log.LstdFlags))
m = append(m, log.New(os.Stderr, severityName[s]+": ", log.LstdFlags))
}
return &loggerT{m: m}
}

func (g *loggerT) Print(l Severity, args ...interface{}) {
switch l {
case InfoLog, WarningLog, ErrorLog:
g.m[l].Print(args...)
case FatalLog:
g.m[InfoLog].Fatal(args...)
}
func (g *loggerT) Info(args ...interface{}) {
g.m[infoLog].Print(args...)
}

func (g *loggerT) Println(l Severity, args ...interface{}) {
switch l {
case InfoLog, WarningLog, ErrorLog:
g.m[l].Println(args...)
case FatalLog:
g.m[InfoLog].Fatalln(args...)
}
func (g *loggerT) Infoln(args ...interface{}) {
g.m[infoLog].Println(args...)
}

func (g *loggerT) Printf(l Severity, format string, args ...interface{}) {
switch l {
case InfoLog, WarningLog, ErrorLog:
g.m[l].Printf(format, args...)
case FatalLog:
g.m[InfoLog].Fatalf(format, args...)
}
func (g *loggerT) Infof(format string, args ...interface{}) {
g.m[infoLog].Printf(format, args...)
}

func (g *loggerT) Warning(args ...interface{}) {
g.m[warningLog].Print(args...)
}

func (g *loggerT) Warningln(args ...interface{}) {
g.m[warningLog].Println(args...)
}

func (g *loggerT) Warningf(format string, args ...interface{}) {
g.m[warningLog].Printf(format, args...)
}

func (g *loggerT) Error(args ...interface{}) {
g.m[errorLog].Print(args...)
}

func (g *loggerT) Errorln(args ...interface{}) {
g.m[errorLog].Println(args...)
}

func (g *loggerT) Errorf(format string, args ...interface{}) {
g.m[errorLog].Printf(format, args...)
}

func (g *loggerT) Fatal(args ...interface{}) {
g.m[fatalLog].Fatal(args...)
}

func (g *loggerT) Fatalln(args ...interface{}) {
g.m[fatalLog].Fatalln(args...)
}

func (g *loggerT) Fatalf(format string, args ...interface{}) {
g.m[fatalLog].Fatalf(format, args...)
}

func (g *loggerT) V(l VerboseLevel) bool {
Expand All @@ -146,78 +183,78 @@ func V(l VerboseLevel) bool {

// Info logs to the INFO log.
func Info(args ...interface{}) {
logger.Print(InfoLog, args...)
logger.Info(args...)
}

// Infof logs to the INFO log. Arguments are handled in the manner of fmt.Printf.
func Infof(format string, args ...interface{}) {
logger.Printf(InfoLog, format, args...)
logger.Infof(format, args...)
}

// Infoln logs to the INFO log. Arguments are handled in the manner of fmt.Println.
func Infoln(args ...interface{}) {
logger.Println(InfoLog, args...)
logger.Infoln(args...)
}

// Warning logs to the WARNING log.
func Warning(args ...interface{}) {
logger.Print(WarningLog, args...)
logger.Warning(args...)
}

// Warningf logs to the WARNING log. Arguments are handled in the manner of fmt.Printf.
func Warningf(format string, args ...interface{}) {
logger.Printf(WarningLog, format, args...)
logger.Warningf(format, args...)
}

// Warningln logs to the WARNING log. Arguments are handled in the manner of fmt.Println.
func Warningln(args ...interface{}) {
logger.Println(WarningLog, args...)
logger.Warningln(args...)
}

// Error logs to the ERROR log.
func Error(args ...interface{}) {
logger.Print(ErrorLog, args...)
logger.Error(args...)
}

// Errorf logs to the ERROR log. Arguments are handled in the manner of fmt.Printf.
func Errorf(format string, args ...interface{}) {
logger.Printf(ErrorLog, format, args...)
logger.Errorf(format, args...)
}

// Errorln logs to the ERROR log. Arguments are handled in the manner of fmt.Println.
func Errorln(args ...interface{}) {
logger.Println(ErrorLog, args...)
logger.Errorln(args...)
}

// Fatal is equivalent to Info() followed by a call to os.Exit() with a non-zero exit code.
func Fatal(args ...interface{}) {
logger.Print(FatalLog, args...)
logger.Fatal(args...)
}

// Fatalf is equivalent to Infof() followed by a call to os.Exit() with a non-zero exit code.
func Fatalf(format string, args ...interface{}) {
logger.Printf(FatalLog, format, args...)
logger.Fatalf(format, args...)
}

// Fatalln is equivalent to Infoln() followed by a call to os.Exit()) with a non-zero exit code.
func Fatalln(args ...interface{}) {
logger.Println(FatalLog, args...)
logger.Fatalln(args...)
}

// Print prints to the logger. Arguments are handled in the manner of fmt.Print.
// Print is deprecated, please use Info.
func Print(args ...interface{}) {
logger.Print(InfoLog, args...)
logger.Info(args...)
}

// Printf prints to the logger. Arguments are handled in the manner of fmt.Printf.
// Printf is deprecated, please use Infof.
func Printf(format string, args ...interface{}) {
logger.Printf(InfoLog, format, args...)
logger.Infof(format, args...)
}

// Println prints to the logger. Arguments are handled in the manner of fmt.Println.
// Println is deprecated, please use Infoln.
func Println(args ...interface{}) {
logger.Println(InfoLog, args...)
logger.Infoln(args...)
}
Loading

0 comments on commit 96ddbb9

Please sign in to comment.