optimization: optimize global logger (#1763)

pull/1765/head
MARATRIX Li 3 years ago committed by GitHub
parent a0006677e9
commit c6c5e4595c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 42
      log/global.go
  2. 2
      transport/grpc/client.go

@ -1,15 +1,11 @@
package log package log
import ( import (
"fmt"
"os"
"sync" "sync"
) )
// globalLogger is designed as a global logger in current process. // globalLogger is designed as a global logger in current process.
var ( var global = &loggerAppliance{}
global = &loggerAppliance{}
)
type loggerAppliance struct { type loggerAppliance struct {
lock sync.Mutex lock sync.Mutex
@ -38,89 +34,87 @@ func SetLogger(logger Logger) {
global.SetLogger(logger) global.SetLogger(logger)
} }
// GetLogger returns global logger in current process.
func GetLogger() Logger { func GetLogger() Logger {
return global.GetLogger() return global.GetLogger()
} }
// Log Print log by level and keyvals. // Log Print log by level and keyvals.
func Log(level Level, keyvals ...interface{}) { func Log(level Level, keyvals ...interface{}) {
_ = global.logger.Log(level, keyvals...) global.helper.Log(level, keyvals...)
} }
// Debug logs a message at debug level. // Debug logs a message at debug level.
func Debug(a ...interface{}) { func Debug(a ...interface{}) {
global.helper.Log(LevelDebug, global.helper.msgKey, fmt.Sprint(a...)) global.helper.Debug(a...)
} }
// Debugf logs a message at debug level. // Debugf logs a message at debug level.
func Debugf(format string, a ...interface{}) { func Debugf(format string, a ...interface{}) {
global.helper.Log(LevelDebug, global.helper.msgKey, fmt.Sprintf(format, a...)) global.helper.Debugf(format, a...)
} }
// Debugw logs a message at debug level. // Debugw logs a message at debug level.
func Debugw(keyvals ...interface{}) { func Debugw(keyvals ...interface{}) {
global.helper.Log(LevelDebug, keyvals...) global.helper.Debugw(keyvals...)
} }
// Info logs a message at info level. // Info logs a message at info level.
func Info(a ...interface{}) { func Info(a ...interface{}) {
global.helper.Log(LevelInfo, global.helper.msgKey, fmt.Sprint(a...)) global.helper.Info(a...)
} }
// Infof logs a message at info level. // Infof logs a message at info level.
func Infof(format string, a ...interface{}) { func Infof(format string, a ...interface{}) {
global.helper.Log(LevelInfo, global.helper.msgKey, fmt.Sprintf(format, a...)) global.helper.Infof(format, a...)
} }
// Infow logs a message at info level. // Infow logs a message at info level.
func Infow(keyvals ...interface{}) { func Infow(keyvals ...interface{}) {
global.helper.Log(LevelInfo, keyvals...) global.helper.Infow(keyvals...)
} }
// Warn logs a message at warn level. // Warn logs a message at warn level.
func Warn(a ...interface{}) { func Warn(a ...interface{}) {
global.helper.Log(LevelWarn, global.helper.msgKey, fmt.Sprint(a...)) global.helper.Warn(a...)
} }
// Warnf logs a message at warnf level. // Warnf logs a message at warnf level.
func Warnf(format string, a ...interface{}) { func Warnf(format string, a ...interface{}) {
global.helper.Log(LevelWarn, global.helper.msgKey, fmt.Sprintf(format, a...)) global.helper.Warnf(format, a...)
} }
// Warnw logs a message at warnf level. // Warnw logs a message at warnf level.
func Warnw(keyvals ...interface{}) { func Warnw(keyvals ...interface{}) {
global.helper.Log(LevelWarn, keyvals...) global.helper.Warnw(keyvals...)
} }
// Error logs a message at error level. // Error logs a message at error level.
func Error(a ...interface{}) { func Error(a ...interface{}) {
global.helper.Log(LevelError, global.helper.msgKey, fmt.Sprint(a...)) global.helper.Error(a...)
} }
// Errorf logs a message at error level. // Errorf logs a message at error level.
func Errorf(format string, a ...interface{}) { func Errorf(format string, a ...interface{}) {
global.helper.Log(LevelError, global.helper.msgKey, fmt.Sprintf(format, a...)) global.helper.Errorf(format, a...)
} }
// Errorw logs a message at error level. // Errorw logs a message at error level.
func Errorw(keyvals ...interface{}) { func Errorw(keyvals ...interface{}) {
global.helper.Log(LevelError, keyvals...) global.helper.Errorw(keyvals...)
} }
// Fatal logs a message at fatal level. // Fatal logs a message at fatal level.
func Fatal(a ...interface{}) { func Fatal(a ...interface{}) {
global.helper.Log(LevelFatal, global.helper.msgKey, fmt.Sprint(a...)) global.helper.Fatal(a...)
os.Exit(1)
} }
// Fatalf logs a message at fatal level. // Fatalf logs a message at fatal level.
func Fatalf(format string, a ...interface{}) { func Fatalf(format string, a ...interface{}) {
global.helper.Log(LevelFatal, global.helper.msgKey, fmt.Sprintf(format, a...)) global.helper.Fatalf(format, a...)
os.Exit(1)
} }
// Fatalw logs a message at fatal level. // Fatalw logs a message at fatal level.
func Fatalw(keyvals ...interface{}) { func Fatalw(keyvals ...interface{}) {
global.helper.Log(LevelFatal, keyvals...) global.helper.Fatalw(keyvals...)
os.Exit(1)
} }

@ -124,7 +124,7 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
options := clientOptions{ options := clientOptions{
timeout: 2000 * time.Millisecond, timeout: 2000 * time.Millisecond,
balancerName: wrr.Name, balancerName: wrr.Name,
logger: log.DefaultLogger, logger: log.GetLogger(),
} }
for _, o := range opts { for _, o := range opts {
o(&options) o(&options)

Loading…
Cancel
Save