logs.go 3.3 KB

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