logs.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package utils
  2. import (
  3. "encoding/json"
  4. "github.com/beego/beego/v2/core/logs"
  5. "github.com/sirupsen/logrus"
  6. "gopkg.in/natefinch/lumberjack.v2"
  7. "os"
  8. "path"
  9. )
  10. const (
  11. DefaultLogPath = "./rdlucklog"
  12. DefaultBinlogPath = "./binlog"
  13. DefaultBinlogFile = "binlog.log"
  14. )
  15. var FileLog = logrus.New()
  16. var ApiLog = logrus.New()
  17. var FileLogData = logrus.New()
  18. var Binlog *logs.BeeLogger
  19. func init() {
  20. logPath := LogPath
  21. if logPath == "" {
  22. logPath = DefaultLogPath
  23. }
  24. logFile := LogFile
  25. if logFile == "" {
  26. logFile = "eta_api.log"
  27. }
  28. os.MkdirAll(logPath, os.ModePerm)
  29. // 打开文件
  30. logFileName := path.Join(logPath, logFile)
  31. // 使用滚动压缩方式记录日志
  32. rolling(FileLog, logFileName)
  33. //rolling(bLogFileName)
  34. // 设置日志输出JSON格式
  35. jsonFormat := new(logrus.JSONFormatter)
  36. jsonFormat.DisableHTMLEscape = true
  37. jsonFormat.TimestampFormat = HlbFormatDateTime
  38. FileLog.SetFormatter(jsonFormat)
  39. FileLog.SetReportCaller(true)
  40. //LogInstance.SetFormatter(&logrus.TextFormatter{})
  41. // 设置日志记录级别
  42. //FileLog.SetLevel(logrus.DebugLevel)
  43. //FileLog.Info("abc")
  44. initBinlog()
  45. initApiLog()
  46. initFileLogData()
  47. }
  48. type logConfig struct {
  49. FileName string `json:"filename" description:"保存的文件名"`
  50. MaxLines int `json:"maxlines" description:"每个文件保存的最大行数,默认值 1000000"`
  51. MaxSize int `json:"maxsize" description:"每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB"`
  52. Daily bool `json:"daily" description:"是否按照每天 logrotate,默认是 true"`
  53. MaxDays int `json:"maxdays" description:"文件最多保存多少天,默认保存 7 天"`
  54. Rotate bool `json:"rotate" description:"是否开启 logrotate,默认是 true"`
  55. Level int `json:"level" description:"日志保存的时候的级别,默认是 Trace 级别"`
  56. Color bool `json:"color" description:"日志是否输出颜色"`
  57. //Perm string `json:"perm" description:"日志文件权限"`
  58. }
  59. func initBinlog() {
  60. //binlog日志
  61. //binlog日志
  62. binlogPath := BinLogPath
  63. if binlogPath == "" {
  64. binlogPath = DefaultBinlogPath
  65. }
  66. binlogFile := BinLogFile
  67. if binlogFile == "" {
  68. binlogFile = DefaultBinlogFile
  69. }
  70. os.MkdirAll(binlogPath, os.ModePerm)
  71. logFileName := path.Join(binlogPath, binlogFile)
  72. Binlog = logs.NewLogger(1000000)
  73. logConf := getDefaultLogConfig()
  74. logConf.FileName = logFileName
  75. logConf.MaxLines = 10000000
  76. logConf.Rotate = true
  77. b, _ := json.Marshal(logConf)
  78. Binlog.SetLogger(logs.AdapterFile, string(b))
  79. Binlog.EnableFuncCallDepth(true)
  80. }
  81. func initApiLog() {
  82. logPath := ApiLogPath
  83. if logPath == "" {
  84. if RunMode == "release" {
  85. logPath = `/data/etalogs/eta_api`
  86. } else {
  87. logPath = `./rdlucklog/api`
  88. }
  89. }
  90. logFile := ApiLogFile
  91. if logFile == "" {
  92. logFile = "eta_api_api.log"
  93. }
  94. os.MkdirAll(logPath, os.ModePerm)
  95. // 打开文件
  96. logFileName := path.Join(logPath, logFile)
  97. // 使用滚动压缩方式记录日志
  98. rolling(ApiLog, logFileName)
  99. //rolling(bLogFileName)
  100. // 设置日志输出JSON格式
  101. jsonFormat := new(logrus.JSONFormatter)
  102. jsonFormat.DisableHTMLEscape = true
  103. jsonFormat.TimestampFormat = HlbFormatDateTime
  104. ApiLog.SetFormatter(jsonFormat)
  105. }
  106. func initFileLogData() {
  107. logPath := LogDataPath
  108. if logPath == "" {
  109. logPath = DefaultLogPath
  110. }
  111. logFile := LogDataFile
  112. if logFile == "" {
  113. logFile = "eta_api_data.log"
  114. }
  115. os.MkdirAll(logPath, os.ModePerm)
  116. // 打开文件
  117. logFileName := path.Join(logPath, logFile)
  118. // 使用滚动压缩方式记录日志
  119. rolling(FileLogData, logFileName)
  120. //rolling(bLogFileName)
  121. // 设置日志输出JSON格式
  122. jsonFormat := new(logrus.JSONFormatter)
  123. jsonFormat.DisableHTMLEscape = true
  124. jsonFormat.TimestampFormat = HlbFormatDateTime
  125. FileLogData.SetFormatter(jsonFormat)
  126. }
  127. // 日志滚动设置
  128. func rolling(fLog *logrus.Logger, logFile string) {
  129. // 设置输出
  130. fLog.SetOutput(&lumberjack.Logger{
  131. Filename: logFile, //日志文件位置
  132. MaxSize: 100, // 单文件最大容量,单位是MB
  133. MaxBackups: 3, // 最大保留过期文件个数
  134. MaxAge: 7, // 保留过期文件的最大时间间隔,单位是天
  135. Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩
  136. LocalTime: true,
  137. })
  138. }
  139. func getDefaultLogConfig() logConfig {
  140. return logConfig{
  141. FileName: "",
  142. MaxLines: 0,
  143. MaxSize: 1 << 28,
  144. Daily: true,
  145. MaxDays: 7, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
  146. Rotate: true,
  147. Level: logs.LevelTrace,
  148. //Perm: "",
  149. }
  150. }