logs.go 1.8 KB

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