From 5cf6309c132fdf1e110fb23f419e6971c63f533c Mon Sep 17 00:00:00 2001 From: gongshangguo <gongshangguo@admin.com> Date: 星期五, 25 二月 2022 18:51:32 +0800 Subject: [PATCH] logger --- logger.go | 93 +++++++++++++++++++++++++++++++++++----------- 1 files changed, 70 insertions(+), 23 deletions(-) diff --git a/logger.go b/logger.go index bf7635f..685626b 100644 --- a/logger.go +++ b/logger.go @@ -13,16 +13,19 @@ logger *zap.SugaredLogger } +var ( + blog *BLog + atomicLevel = zap.NewAtomicLevelAt(zapcore.DebugLevel) +) + func (l *BLog) Write(buf []byte) (n int, err error) { - l.logger.Debug(string(buf)) + l.logger.Debug(strings.Replace(string(buf),"\n","", -1)) return len(buf),nil } func GetLogFile() *BLog { return blog } - -var blog *BLog const ( DebugLevel = iota -1 @@ -33,17 +36,39 @@ PanicLevel FatalLevel ) -func InitLogger(logPath string, logLevel int,maxSize int, maxBackups int, maxAge int) { + +func InitLogger(logPath string, logLevel int,maxSize int, maxBackups int, maxAge int) *zap.SugaredLogger { logdir := "./logger/" + logName := "" if index := strings.LastIndex(logPath, "/"); index != -1 { logdir = logPath[0:index] + "/" + if index < len(logPath)-1 { + logName = logPath[index+1:] + } + } else { + logName = logPath + } + if logName == "" { + logName = "log.log" } fi,err := os.Stat(logdir) if !((err == nil || os.IsExist(err)) && fi.IsDir()) { os.MkdirAll(logdir, os.ModePerm) } + if logLevel < DebugLevel || logLevel > FatalLevel { + logLevel = DebugLevel + } + if maxSize <=0 { + maxSize = 128 + } + if maxBackups <=0 { + maxBackups = 30 + } + if maxAge <= 0 { + maxAge = 15 + } hook := lumberjack.Logger { - Filename: logPath, //鏃ュ織鏂囦欢鐨勪綅缃� + Filename: logdir+logName, //鏃ュ織鏂囦欢鐨勪綅缃� MaxSize: maxSize, //鍦ㄨ繘琛屽垏鍓蹭箣鍓嶏紝鏃ュ織鏂囦欢鐨勬渶澶уぇ灏忥紙浠B涓哄崟浣嶏級 MaxBackups: maxBackups, //淇濈暀鏃ф枃浠剁殑鏈�澶т釜鏁� MaxAge: maxAge, //淇濈暀鏃ф枃浠剁殑鏈�澶уぉ鏁� @@ -52,6 +77,32 @@ } w := zapcore.AddSync(&hook) + atomicLevel = zap.NewAtomicLevelAt(getZapLevel(logLevel)) + + encoderConfig := zap.NewProductionEncoderConfig() + encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder + encoderConfig.CallerKey = "file" + encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder + encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(t.Format("2006-01-02 15:04:05")) + } + + core := zapcore.NewCore( + zapcore.NewConsoleEncoder(encoderConfig), + w, + atomicLevel, + ) + + log := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1)) + + blog = &BLog{ + logger: log.Sugar(), + } + blog.logger.Info("init logger success") + return blog.logger +} + +func getZapLevel(logLevel int) zapcore.Level { var level zapcore.Level switch logLevel { case DebugLevel: @@ -71,24 +122,12 @@ default: level = zap.InfoLevel } - encoderConfig := zap.NewProductionEncoderConfig() - encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder - encoderConfig.CallerKey = "file" - encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder - encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) { - enc.AppendString(t.Format("2006-01-02 15:04:05")) - } + return level +} - core := zapcore.NewCore( - zapcore.NewConsoleEncoder(encoderConfig), - w, - level, - ) - log := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1)) - blog = &BLog{ - logger: log.Sugar(), - } - blog.logger.Info("init logger success") +func SetLevel(lv int) error { + atomicLevel.SetLevel(getZapLevel(lv)) + return nil } func Debug(v ...interface{}) { @@ -121,4 +160,12 @@ func Errorf(template string, v ...interface{}) { blog.logger.Errorf(template, v...) -} \ No newline at end of file +} + +func Fatal(v ...interface{}) { + blog.logger.Fatal(v...) +} + +func Fatalf(template string, v ...interface{}) { + blog.logger.Fatalf(template, v...) +} -- Gitblit v1.8.0