logs.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_mobile.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. //初始化binlog日志
  44. //FileLog.Info("abc")
  45. initBinlog()
  46. initApiLog()
  47. initFileLogData()
  48. }
  49. type logConfig struct {
  50. FileName string `json:"filename" description:"保存的文件名"`
  51. MaxLines int `json:"maxlines" description:"每个文件保存的最大行数,默认值 1000000"`
  52. MaxSize int `json:"maxsize" description:"每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB"`
  53. Daily bool `json:"daily" description:"是否按照每天 logrotate,默认是 true"`
  54. MaxDays int `json:"maxdays" description:"文件最多保存多少天,默认保存 7 天"`
  55. Rotate bool `json:"rotate" description:"是否开启 logrotate,默认是 true"`
  56. Level int `json:"level" description:"日志保存的时候的级别,默认是 Trace 级别"`
  57. Color bool `json:"color" description:"日志是否输出颜色"`
  58. //Perm string `json:"perm" description:"日志文件权限"`
  59. }
  60. func initBinlog() {
  61. //binlog日志
  62. //binlog日志
  63. binlogPath := BinLogPath
  64. if binlogPath == "" {
  65. binlogPath = DefaultBinlogPath
  66. }
  67. binlogFile := BinLogFile
  68. if binlogFile == "" {
  69. binlogFile = DefaultBinlogFile
  70. }
  71. os.MkdirAll(binlogPath, os.ModePerm)
  72. logFileName := path.Join(binlogPath, binlogFile)
  73. Binlog = logs.NewLogger(1000000)
  74. logConf := getDefaultLogConfig()
  75. logConf.FileName = logFileName
  76. logConf.MaxLines = 10000000
  77. logConf.Rotate = true
  78. b, _ := json.Marshal(logConf)
  79. Binlog.SetLogger(logs.AdapterFile, string(b))
  80. Binlog.EnableFuncCallDepth(true)
  81. }
  82. func initApiLog() {
  83. logPath := ApiLogPath
  84. if logPath == "" {
  85. if RunMode == "release" {
  86. logPath = `/data/etalogs/eta_mobile`
  87. } else {
  88. logPath = `./rdlucklog/api`
  89. }
  90. }
  91. logFile := ApiLogFile
  92. if logFile == "" {
  93. logFile = "eta_mobile_api.log"
  94. }
  95. os.MkdirAll(logPath, os.ModePerm)
  96. // 打开文件
  97. logFileName := path.Join(logPath, logFile)
  98. // 使用滚动压缩方式记录日志
  99. rolling(ApiLog, logFileName)
  100. //rolling(bLogFileName)
  101. // 设置日志输出JSON格式
  102. jsonFormat := new(logrus.JSONFormatter)
  103. jsonFormat.DisableHTMLEscape = true
  104. jsonFormat.TimestampFormat = HlbFormatDateTime
  105. ApiLog.SetFormatter(jsonFormat)
  106. }
  107. func initFileLogData() {
  108. logPath := LogDataPath
  109. if logPath == "" {
  110. logPath = DefaultLogPath
  111. }
  112. logFile := LogDataFile
  113. if logFile == "" {
  114. logFile = "eta_mobile_data.log"
  115. }
  116. os.MkdirAll(logPath, os.ModePerm)
  117. // 打开文件
  118. logFileName := path.Join(logPath, logFile)
  119. // 使用滚动压缩方式记录日志
  120. rolling(FileLogData, logFileName)
  121. //rolling(bLogFileName)
  122. // 设置日志输出JSON格式
  123. jsonFormat := new(logrus.JSONFormatter)
  124. jsonFormat.DisableHTMLEscape = true
  125. jsonFormat.TimestampFormat = HlbFormatDateTime
  126. FileLogData.SetFormatter(jsonFormat)
  127. }
  128. // 日志滚动设置
  129. func rolling(fLog *logrus.Logger, logFile string) {
  130. // 设置输出
  131. fLog.SetOutput(&lumberjack.Logger{
  132. Filename: logFile, //日志文件位置
  133. MaxSize: 100, // 单文件最大容量,单位是MB
  134. MaxBackups: 3, // 最大保留过期文件个数
  135. MaxAge: 7, // 保留过期文件的最大时间间隔,单位是天
  136. Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩
  137. LocalTime: true,
  138. })
  139. }
  140. func getDefaultLogConfig() logConfig {
  141. return logConfig{
  142. FileName: "",
  143. MaxLines: 0,
  144. MaxSize: 1 << 28,
  145. Daily: true,
  146. MaxDays: 7, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
  147. Rotate: true,
  148. Level: logs.LevelTrace,
  149. //Perm: "",
  150. }
  151. }