logs.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package utils
  2. import (
  3. "github.com/sirupsen/logrus"
  4. "gopkg.in/natefinch/lumberjack.v2"
  5. "os"
  6. "path"
  7. )
  8. const (
  9. DefaultLogPath = "./rdlucklog"
  10. DefaultBinlogPath = "./binlog"
  11. DefaultBinlogFile = "binlog.log"
  12. )
  13. var FileLog = logrus.New()
  14. var Binlog = logrus.New()
  15. var ApiLog = logrus.New()
  16. var FileLogData = logrus.New()
  17. func init() {
  18. logPath := LogPath
  19. if logPath == "" {
  20. logPath = DefaultLogPath
  21. }
  22. logFile := LogFile
  23. if logFile == "" {
  24. logFile = "eta_api.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. initApiLog()
  44. }
  45. func initBinlog() {
  46. //binlog日志
  47. binlogPath := BinLogPath
  48. if binlogPath == "" {
  49. binlogPath = DefaultBinlogPath
  50. }
  51. binlogFile := BinLogFile
  52. if binlogFile == "" {
  53. binlogFile = DefaultBinlogFile
  54. }
  55. os.MkdirAll(binlogPath, os.ModePerm)
  56. // 使用滚动压缩方式记录日志
  57. logFileName := path.Join(binlogPath, binlogFile)
  58. rolling(Binlog, logFileName)
  59. //rolling(bLogFileName)
  60. // 设置日志输出JSON格式
  61. jsonFormat := new(logrus.JSONFormatter)
  62. jsonFormat.DisableHTMLEscape = true
  63. jsonFormat.TimestampFormat = HlbFormatDateTime
  64. Binlog.SetFormatter(jsonFormat)
  65. }
  66. func initApiLog() {
  67. logPath := ApiLogPath
  68. if logPath == "" {
  69. if RunMode == "release" {
  70. logPath = `/data/etalogs/eta_api`
  71. } else {
  72. logPath = `./rdlucklog/api`
  73. }
  74. }
  75. logFile := ApiLogFile
  76. if logFile == "" {
  77. logFile = "eta_api_api.log"
  78. }
  79. os.MkdirAll(logPath, os.ModePerm)
  80. // 打开文件
  81. logFileName := path.Join(logPath, logFile)
  82. // 使用滚动压缩方式记录日志
  83. rolling(ApiLog, logFileName)
  84. //rolling(bLogFileName)
  85. // 设置日志输出JSON格式
  86. jsonFormat := new(logrus.JSONFormatter)
  87. jsonFormat.DisableHTMLEscape = true
  88. jsonFormat.TimestampFormat = HlbFormatDateTime
  89. ApiLog.SetFormatter(jsonFormat)
  90. }
  91. func initFileLogData() {
  92. logPath := LogDataPath
  93. if logPath == "" {
  94. logPath = DefaultLogPath
  95. }
  96. logFile := LogDataFile
  97. if logFile == "" {
  98. logFile = "eta_api_data.log"
  99. }
  100. os.MkdirAll(logPath, os.ModePerm)
  101. // 打开文件
  102. logFileName := path.Join(logPath, logFile)
  103. // 使用滚动压缩方式记录日志
  104. rolling(FileLogData, logFileName)
  105. //rolling(bLogFileName)
  106. // 设置日志输出JSON格式
  107. jsonFormat := new(logrus.JSONFormatter)
  108. jsonFormat.DisableHTMLEscape = true
  109. jsonFormat.TimestampFormat = HlbFormatDateTime
  110. FileLogData.SetFormatter(jsonFormat)
  111. }
  112. // 日志滚动设置
  113. func rolling(fLog *logrus.Logger, logFile string) {
  114. // 设置输出
  115. fLog.SetOutput(&lumberjack.Logger{
  116. Filename: logFile, //日志文件位置
  117. MaxSize: 100, // 单文件最大容量,单位是MB
  118. MaxBackups: 3, // 最大保留过期文件个数
  119. MaxAge: 7, // 保留过期文件的最大时间间隔,单位是天
  120. Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩
  121. LocalTime: true,
  122. })
  123. }