lclog.go 954 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package logger
  2. import (
  3. "github.com/druidcaesa/gotool"
  4. rotatelogs "github.com/lestrrat/go-file-rotatelogs"
  5. "github.com/sirupsen/logrus"
  6. "os"
  7. "path"
  8. "server/edge/util/config"
  9. "time"
  10. )
  11. var Logger *logrus.Logger
  12. func InitLog() {
  13. logFilePath := config.Instance().Logger.Path
  14. logFileName := config.Instance().Logger.Name
  15. err := os.MkdirAll(logFilePath, os.ModeDir)
  16. if err != nil {
  17. gotool.Logs.ErrorLog().Println(err)
  18. panic(err)
  19. }
  20. // 日志文件
  21. fileName := path.Join(logFilePath, logFileName)
  22. writer, _ := rotatelogs.New(
  23. fileName+".%Y%m%d.log",
  24. rotatelogs.WithMaxAge(2*24*time.Hour), // 文件最大保存时间
  25. rotatelogs.WithRotationTime(24*time.Hour), // 日志切割时间间隔
  26. )
  27. // 实例化
  28. logger := logrus.New()
  29. logger.SetFormatter(&logrus.JSONFormatter{
  30. TimestampFormat: "2006-01-02 15:04:05.000",
  31. })
  32. // 设置日志级别
  33. logger.SetLevel(logrus.DebugLevel)
  34. logger.SetOutput(writer)
  35. Logger = logger
  36. }