logs.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. func init() {
  17. logPath := LogPath
  18. if logPath == "" {
  19. logPath = DefaultLogPath
  20. }
  21. logFile := LogFile
  22. if logFile == "" {
  23. logFile = APP_NAME_EN + ".log"
  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. jsonFormat.TimestampFormat = HlbFormatDateTime
  35. FileLog.SetFormatter(jsonFormat)
  36. FileLog.SetReportCaller(true)
  37. //LogInstance.SetFormatter(&logrus.TextFormatter{})
  38. // 设置日志记录级别
  39. //FileLog.SetLevel(logrus.DebugLevel)
  40. //FileLog.Info("abc")
  41. initBinlog()
  42. initApiLog()
  43. }
  44. func initBinlog() {
  45. //binlog日志
  46. binlogPath := BinLogPath
  47. if binlogPath == "" {
  48. binlogPath = DefaultBinlogPath
  49. }
  50. binlogFile := BinLogFile
  51. if binlogFile == "" {
  52. binlogFile = DefaultBinlogFile
  53. }
  54. os.MkdirAll(binlogPath, os.ModePerm)
  55. // 使用滚动压缩方式记录日志
  56. logFileName := path.Join(binlogPath, binlogFile)
  57. rolling(Binlog, logFileName)
  58. //rolling(bLogFileName)
  59. // 设置日志输出JSON格式
  60. jsonFormat := new(logrus.JSONFormatter)
  61. jsonFormat.DisableHTMLEscape = true
  62. jsonFormat.TimestampFormat = HlbFormatDateTime
  63. Binlog.SetFormatter(jsonFormat)
  64. }
  65. func initApiLog() {
  66. logPath := ApiLogPath
  67. if logPath == "" {
  68. if RunMode == "release" {
  69. logPath = `/data/etalogs/` + APP_NAME_EN
  70. } else {
  71. logPath = `./rdlucklog/api`
  72. }
  73. }
  74. logFile := ApiLogFile
  75. if logFile == "" {
  76. logFile = APP_NAME_EN + "_api.log"
  77. }
  78. os.MkdirAll(logPath, os.ModePerm)
  79. // 打开文件
  80. logFileName := path.Join(logPath, logFile)
  81. // 使用滚动压缩方式记录日志
  82. rolling(ApiLog, logFileName)
  83. //rolling(bLogFileName)
  84. // 设置日志输出JSON格式
  85. jsonFormat := new(logrus.JSONFormatter)
  86. jsonFormat.DisableHTMLEscape = true
  87. jsonFormat.TimestampFormat = HlbFormatDateTime
  88. ApiLog.SetFormatter(jsonFormat)
  89. }
  90. // 日志滚动设置
  91. func rolling(fLog *logrus.Logger, logFile string) {
  92. // 设置输出
  93. fLog.SetOutput(&lumberjack.Logger{
  94. Filename: logFile, //日志文件位置
  95. MaxSize: 100, // 单文件最大容量,单位是MB
  96. MaxBackups: 3, // 最大保留过期文件个数
  97. MaxAge: 7, // 保留过期文件的最大时间间隔,单位是天
  98. Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩
  99. LocalTime: true,
  100. })
  101. }