logs.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package utils
  2. import (
  3. "github.com/sirupsen/logrus"
  4. "gopkg.in/natefinch/lumberjack.v2"
  5. "os"
  6. "path"
  7. )
  8. var (
  9. DefaultLogPath = "./rdlucklog"
  10. DefaultLogFile = "eta_task.log"
  11. DefaultBinlogPath = "./binlog"
  12. DefaultBinlogFile = "binlog.log"
  13. )
  14. var FileLog = logrus.New()
  15. var Binlog = logrus.New()
  16. func init() {
  17. logPath := LogPath
  18. if logPath == "" {
  19. logPath = DefaultLogPath
  20. }
  21. logFile := LogFile
  22. if logFile == "" {
  23. logFile = DefaultLogFile
  24. }
  25. os.MkdirAll(logPath, os.ModePerm)
  26. // 打开文件
  27. logFileName := path.Join(logPath, logFile)
  28. // 使用滚动压缩方式记录日志
  29. rolling(FileLog, logFileName)
  30. //rolling(bLogFileName)
  31. // 设置日志输出JSON格式
  32. jsonFormat := new(logrus.JSONFormatter)
  33. jsonFormat.DisableHTMLEscape = true
  34. FileLog.SetFormatter(jsonFormat)
  35. //LogInstance.SetFormatter(&logrus.TextFormatter{})
  36. // 设置日志记录级别
  37. //FileLog.SetLevel(logrus.DebugLevel)
  38. //FileLog.Info("abc")
  39. initBinlog()
  40. }
  41. func initBinlog() {
  42. //binlog日志
  43. binlogPath := BinLogPath
  44. if binlogPath == "" {
  45. binlogPath = DefaultBinlogPath
  46. }
  47. binlogFile := BinLogFile
  48. if binlogFile == "" {
  49. binlogFile = DefaultBinlogFile
  50. }
  51. os.MkdirAll(binlogPath, os.ModePerm)
  52. // 使用滚动压缩方式记录日志
  53. logFileName := path.Join(binlogPath, binlogFile)
  54. rolling(Binlog, logFileName)
  55. //rolling(bLogFileName)
  56. // 设置日志输出JSON格式
  57. jsonFormat := new(logrus.JSONFormatter)
  58. jsonFormat.DisableHTMLEscape = true
  59. Binlog.SetFormatter(jsonFormat)
  60. }
  61. // 日志滚动设置
  62. func rolling(fLog *logrus.Logger, logFile string) {
  63. // 设置输出
  64. fLog.SetOutput(&lumberjack.Logger{
  65. Filename: logFile, //日志文件位置
  66. MaxSize: 500, // 单文件最大容量,单位是MB
  67. MaxBackups: 3, // 最大保留过期文件个数
  68. MaxAge: 7, // 保留过期文件的最大时间间隔,单位是天
  69. Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩
  70. })
  71. }