gongshangguo
2022-02-25 5cf6309c132fdf1e110fb23f419e6971c63f533c
logger.go
@@ -4,10 +4,28 @@
   "github.com/natefinch/lumberjack"
   "go.uber.org/zap"
   "go.uber.org/zap/zapcore"
   "os"
   "strings"
   "time"
)
var logger *zap.SugaredLogger
type BLog struct {
   logger *zap.SugaredLogger
}
var (
   blog *BLog
   atomicLevel = zap.NewAtomicLevelAt(zapcore.DebugLevel)
)
func (l *BLog) Write(buf []byte) (n int, err error) {
   l.logger.Debug(strings.Replace(string(buf),"\n","", -1))
   return len(buf),nil
}
func GetLogFile() *BLog {
   return  blog
}
const (
   DebugLevel = iota -1
@@ -18,9 +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,       //在进行切割之前,日志文件的最大大小(以MB为单位)
      MaxBackups: maxBackups,     //保留旧文件的最大个数
      MaxAge: maxAge,           //保留旧文件的最大天数
@@ -29,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:
@@ -48,52 +122,50 @@
   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))
   logger = log.Sugar()
   logger.Info("init logger success")
func SetLevel(lv int) error {
   atomicLevel.SetLevel(getZapLevel(lv))
   return nil
}
func Debug(v ...interface{}) {
   logger.Debug(v...)
   blog.logger.Debug(v...)
}
func Debugf(template string, v ...interface{}) {
   logger.Debugf(template, v...)
   blog.logger.Debugf(template, v...)
}
func Info(v ...interface{}) {
   logger.Info(v)
   blog.logger.Info(v)
}
func Infof(template string, v ...interface{}) {
   logger.Infof(template, v...)
   blog.logger.Infof(template, v...)
}
func Warn(v ...interface{}) {
   logger.Warn(v...)
   blog.logger.Warn(v...)
}
func Warnf(template string, v ...interface{}) {
   logger.Warnf(template, v...)
   blog.logger.Warnf(template, v...)
}
func Error(v ...interface{}) {
   logger.Error(v...)
   blog.logger.Error(v...)
}
func Errorf(template string, v ...interface{}) {
   logger.Errorf(template, v...)
}
   blog.logger.Errorf(template, v...)
}
func Fatal(v ...interface{}) {
   blog.logger.Fatal(v...)
}
func Fatalf(template string, v ...interface{}) {
   blog.logger.Fatalf(template, v...)
}