logs.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package utils
  2. import (
  3. "encoding/json"
  4. "github.com/beego/beego/v2/core/logs"
  5. "github.com/sirupsen/logrus"
  6. "gopkg.in/natefinch/lumberjack.v2"
  7. "os"
  8. "path"
  9. )
  10. const (
  11. DefaultLogPath = "./rdlucklog"
  12. DefaultBinlogPath = "./binlog"
  13. DefaultBinlogFile = "binlog.log"
  14. )
  15. var FileLog = logrus.New()
  16. var Binlog *logs.BeeLogger
  17. func init() {
  18. logPath := LogPath
  19. if logPath == "" {
  20. logPath = DefaultLogPath
  21. }
  22. logFile := LogFile
  23. if logFile == "" {
  24. logFile = "eta_task.log"
  25. }
  26. os.MkdirAll(logPath, os.ModePerm)
  27. // 打开文件
  28. logFileName := path.Join(logPath, logFile)
  29. // 使用滚动压缩方式记录日志
  30. rolling(FileLog, logFileName)
  31. //rolling(bLogFileName)
  32. // 设置日志输出JSON格式
  33. jsonFormat := new(logrus.JSONFormatter)
  34. jsonFormat.DisableHTMLEscape = true
  35. jsonFormat.TimestampFormat = HlbFormatDateTime
  36. FileLog.SetFormatter(jsonFormat)
  37. FileLog.SetReportCaller(true)
  38. //LogInstance.SetFormatter(&logrus.TextFormatter{})
  39. // 设置日志记录级别
  40. //FileLog.SetLevel(logrus.DebugLevel)
  41. //FileLog.Info("abc")
  42. initBinlog()
  43. }
  44. type logConfig struct {
  45. FileName string `json:"filename" description:"保存的文件名"`
  46. MaxLines int `json:"maxlines" description:"每个文件保存的最大行数,默认值 1000000"`
  47. MaxSize int `json:"maxsize" description:"每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB"`
  48. Daily bool `json:"daily" description:"是否按照每天 logrotate,默认是 true"`
  49. MaxDays int `json:"maxdays" description:"文件最多保存多少天,默认保存 7 天"`
  50. Rotate bool `json:"rotate" description:"是否开启 logrotate,默认是 true"`
  51. Level int `json:"level" description:"日志保存的时候的级别,默认是 Trace 级别"`
  52. Color bool `json:"color" description:"日志是否输出颜色"`
  53. //Perm string `json:"perm" description:"日志文件权限"`
  54. }
  55. func initBinlog() {
  56. //binlog日志
  57. //binlog日志
  58. binlogPath := BinLogPath
  59. if binlogPath == "" {
  60. binlogPath = DefaultBinlogPath
  61. }
  62. binlogFile := BinLogFile
  63. if binlogFile == "" {
  64. binlogFile = DefaultBinlogFile
  65. }
  66. os.MkdirAll(binlogPath, os.ModePerm)
  67. logFileName := path.Join(binlogPath, binlogFile)
  68. Binlog = logs.NewLogger(1000000)
  69. logConf := getDefaultLogConfig()
  70. logConf.FileName = logFileName
  71. logConf.MaxLines = 10000000
  72. logConf.Rotate = true
  73. b, _ := json.Marshal(logConf)
  74. Binlog.SetLogger(logs.AdapterFile, string(b))
  75. Binlog.EnableFuncCallDepth(true)
  76. }
  77. // 日志滚动设置
  78. func rolling(fLog *logrus.Logger, logFile string) {
  79. // 设置输出
  80. fLog.SetOutput(&lumberjack.Logger{
  81. Filename: logFile, //日志文件位置
  82. MaxSize: 100, // 单文件最大容量,单位是MB
  83. MaxBackups: 3, // 最大保留过期文件个数
  84. MaxAge: 7, // 保留过期文件的最大时间间隔,单位是天
  85. Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩
  86. LocalTime: true,
  87. })
  88. }
  89. func getDefaultLogConfig() logConfig {
  90. return logConfig{
  91. FileName: "",
  92. MaxLines: 0,
  93. MaxSize: 1 << 28,
  94. Daily: true,
  95. MaxDays: 7, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
  96. Rotate: true,
  97. Level: logs.LevelTrace,
  98. //Perm: "",
  99. }
  100. }