mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
优化日志
This commit is contained in:
49
log/log.go
49
log/log.go
@@ -22,11 +22,11 @@ type Logger struct {
|
|||||||
LogLevel zapcore.Level
|
LogLevel zapcore.Level
|
||||||
Encoder zapcore.Encoder
|
Encoder zapcore.Encoder
|
||||||
LogConfig *lumberjack.Logger
|
LogConfig *lumberjack.Logger
|
||||||
sugaredLogger *zap.SugaredLogger
|
SugaredLogger *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetLogger(logger *Logger) {
|
func SetLogger(logger *Logger) {
|
||||||
if logger != nil && isSetLogger == false {
|
if logger != nil {
|
||||||
gLogger = logger
|
gLogger = logger
|
||||||
isSetLogger = true
|
isSetLogger = true
|
||||||
}
|
}
|
||||||
@@ -95,13 +95,16 @@ func (logger *Logger) Enabled(zapcore.Level) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) Init() {
|
func (logger *Logger) Init() {
|
||||||
var coreList []zapcore.Core
|
if isSetLogger {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var coreList []zapcore.Core
|
||||||
if logger.OpenConsole == nil || *logger.OpenConsole {
|
if logger.OpenConsole == nil || *logger.OpenConsole {
|
||||||
core := zapcore.NewCore(logger.Encoder, zapcore.AddSync(os.Stdout), logger.LogLevel)
|
core := zapcore.NewCore(logger.Encoder, zapcore.AddSync(os.Stdout), logger.LogLevel)
|
||||||
coreList = append(coreList, core)
|
coreList = append(coreList, core)
|
||||||
}
|
}
|
||||||
|
|
||||||
if logger.LogPath != "" {
|
if logger.LogPath != "" {
|
||||||
writeSyncer := zapcore.AddSync(logger.LogConfig)
|
writeSyncer := zapcore.AddSync(logger.LogConfig)
|
||||||
core := zapcore.NewCore(logger.Encoder, writeSyncer, logger.LogLevel)
|
core := zapcore.NewCore(logger.Encoder, writeSyncer, logger.LogLevel)
|
||||||
@@ -110,7 +113,7 @@ func (logger *Logger) Init() {
|
|||||||
|
|
||||||
core := zapcore.NewTee(coreList...)
|
core := zapcore.NewTee(coreList...)
|
||||||
logger.Logger = zap.New(core, zap.AddCaller(), zap.AddStacktrace(logger), zap.AddCallerSkip(1+logger.Skip))
|
logger.Logger = zap.New(core, zap.AddCaller(), zap.AddStacktrace(logger), zap.AddCallerSkip(1+logger.Skip))
|
||||||
logger.sugaredLogger = logger.Logger.Sugar()
|
logger.SugaredLogger = logger.Logger.Sugar()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) Debug(msg string, fields ...zap.Field) {
|
func (logger *Logger) Debug(msg string, fields ...zap.Field) {
|
||||||
@@ -170,84 +173,84 @@ func Fatal(msg string, fields ...zap.Field) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Debugf(template string, args ...any) {
|
func Debugf(template string, args ...any) {
|
||||||
gLogger.sugaredLogger.Debugf(template, args...)
|
gLogger.SugaredLogger.Debugf(template, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Infof(template string, args ...any) {
|
func Infof(template string, args ...any) {
|
||||||
gLogger.sugaredLogger.Infof(template, args...)
|
gLogger.SugaredLogger.Infof(template, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Warnf(template string, args ...any) {
|
func Warnf(template string, args ...any) {
|
||||||
gLogger.sugaredLogger.Warnf(template, args...)
|
gLogger.SugaredLogger.Warnf(template, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Errorf(template string, args ...any) {
|
func Errorf(template string, args ...any) {
|
||||||
gLogger.sugaredLogger.Errorf(template, args...)
|
gLogger.SugaredLogger.Errorf(template, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func StackErrorf(template string, args ...any) {
|
func StackErrorf(template string, args ...any) {
|
||||||
gLogger.stack = true
|
gLogger.stack = true
|
||||||
gLogger.sugaredLogger.Errorf(template, args...)
|
gLogger.SugaredLogger.Errorf(template, args...)
|
||||||
gLogger.stack = false
|
gLogger.stack = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func Fatalf(template string, args ...any) {
|
func Fatalf(template string, args ...any) {
|
||||||
gLogger.sugaredLogger.Fatalf(template, args...)
|
gLogger.SugaredLogger.Fatalf(template, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) SDebug(args ...interface{}) {
|
func (logger *Logger) SDebug(args ...interface{}) {
|
||||||
logger.sugaredLogger.Debugln(args...)
|
logger.SugaredLogger.Debugln(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) SInfo(args ...interface{}) {
|
func (logger *Logger) SInfo(args ...interface{}) {
|
||||||
logger.sugaredLogger.Infoln(args...)
|
logger.SugaredLogger.Infoln(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) SWarn(args ...interface{}) {
|
func (logger *Logger) SWarn(args ...interface{}) {
|
||||||
logger.sugaredLogger.Warnln(args...)
|
logger.SugaredLogger.Warnln(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) SError(args ...interface{}) {
|
func (logger *Logger) SError(args ...interface{}) {
|
||||||
logger.sugaredLogger.Errorln(args...)
|
logger.SugaredLogger.Errorln(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) SStackError(args ...interface{}) {
|
func (logger *Logger) SStackError(args ...interface{}) {
|
||||||
gLogger.stack = true
|
gLogger.stack = true
|
||||||
logger.sugaredLogger.Errorln(args...)
|
logger.SugaredLogger.Errorln(args...)
|
||||||
gLogger.stack = false
|
gLogger.stack = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) SFatal(args ...interface{}) {
|
func (logger *Logger) SFatal(args ...interface{}) {
|
||||||
gLogger.stack = true
|
gLogger.stack = true
|
||||||
logger.sugaredLogger.Fatalln(args...)
|
logger.SugaredLogger.Fatalln(args...)
|
||||||
gLogger.stack = false
|
gLogger.stack = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func SDebug(args ...interface{}) {
|
func SDebug(args ...interface{}) {
|
||||||
gLogger.sugaredLogger.Debugln(args...)
|
gLogger.SugaredLogger.Debugln(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SInfo(args ...interface{}) {
|
func SInfo(args ...interface{}) {
|
||||||
gLogger.sugaredLogger.Infoln(args...)
|
gLogger.SugaredLogger.Infoln(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SWarn(args ...interface{}) {
|
func SWarn(args ...interface{}) {
|
||||||
gLogger.sugaredLogger.Warnln(args...)
|
gLogger.SugaredLogger.Warnln(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SError(args ...interface{}) {
|
func SError(args ...interface{}) {
|
||||||
gLogger.sugaredLogger.Errorln(args...)
|
gLogger.SugaredLogger.Errorln(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SStackError(args ...interface{}) {
|
func SStackError(args ...interface{}) {
|
||||||
gLogger.stack = true
|
gLogger.stack = true
|
||||||
gLogger.sugaredLogger.Errorln(args...)
|
gLogger.SugaredLogger.Errorln(args...)
|
||||||
gLogger.stack = false
|
gLogger.stack = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func SFatal(args ...interface{}) {
|
func SFatal(args ...interface{}) {
|
||||||
gLogger.stack = true
|
gLogger.stack = true
|
||||||
gLogger.sugaredLogger.Fatalln(args...)
|
gLogger.SugaredLogger.Fatalln(args...)
|
||||||
gLogger.stack = false
|
gLogger.stack = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user