logs.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package utils
  2. import (
  3. "encoding/json"
  4. "github.com/beego/beego/v2/core/logs"
  5. rotatelogs "github.com/lestrrat-go/file-rotatelogs"
  6. "github.com/sirupsen/logrus"
  7. "os"
  8. "path"
  9. "time"
  10. )
  11. const (
  12. DefaultLogPath = "./etalogs/filelog"
  13. DefaultBinlogPath = "./etalogs/binlog"
  14. DefaultDataPath = "./etalogs/datalog"
  15. DefaultApiLogPath = "./etalogs/apilog"
  16. )
  17. var FileLog = logrus.New()
  18. var ApiLog = logrus.New()
  19. var FileLogData = logrus.New()
  20. var Binlog *logs.BeeLogger
  21. func init() {
  22. if LogMaxDays == 0 {
  23. LogMaxDays = 30
  24. }
  25. logPath := LogPath
  26. if logPath == "" {
  27. logPath = DefaultLogPath
  28. }
  29. logFile := LogFile
  30. if logFile == "" {
  31. logFile = "filelog.log"
  32. }
  33. os.MkdirAll(logPath, os.ModePerm)
  34. // 打开文件
  35. logFileName := path.Join(logPath, logFile)
  36. rolling(FileLog, logFileName, LogMaxDays)
  37. // 设置日志输出JSON格式
  38. jsonFormat := new(logrus.JSONFormatter)
  39. jsonFormat.DisableHTMLEscape = true
  40. jsonFormat.TimestampFormat = HlbFormatDateTime
  41. FileLog.SetFormatter(jsonFormat)
  42. FileLog.SetReportCaller(true)
  43. //LogInstance.SetFormatter(&logrus.TextFormatter{})
  44. // 设置日志记录级别
  45. //FileLog.SetLevel(logrus.DebugLevel)
  46. initBinlog()
  47. initApiLog()
  48. initFileLogData()
  49. }
  50. type logConfig struct {
  51. FileName string `json:"filename" description:"保存的文件名"`
  52. MaxLines int `json:"maxlines" description:"每个文件保存的最大行数,默认值 1000000"`
  53. MaxSize int `json:"maxsize" description:"每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB"`
  54. Daily bool `json:"daily" description:"是否按照每天 logrotate,默认是 true"`
  55. MaxDays int `json:"maxdays" description:"文件最多保存多少天,默认保存 7 天"`
  56. Rotate bool `json:"rotate" description:"是否开启 logrotate,默认是 true"`
  57. Level int `json:"level" description:"日志保存的时候的级别,默认是 Trace 级别"`
  58. Color bool `json:"color" description:"日志是否输出颜色"`
  59. //Perm string `json:"perm" description:"日志文件权限"`
  60. }
  61. func initBinlog() {
  62. //binlog日志
  63. //binlog日志
  64. binlogPath := BinLogPath
  65. if binlogPath == "" {
  66. binlogPath = DefaultBinlogPath
  67. }
  68. binlogFile := BinLogFile
  69. if binlogFile == "" {
  70. binlogFile = "binlog.log"
  71. }
  72. os.MkdirAll(binlogPath, os.ModePerm)
  73. logFileName := path.Join(binlogPath, binlogFile)
  74. Binlog = logs.NewLogger(1000000)
  75. logConf := getDefaultLogConfig()
  76. logConf.FileName = logFileName
  77. logConf.MaxLines = 10000000
  78. logConf.Rotate = true
  79. b, _ := json.Marshal(logConf)
  80. Binlog.SetLogger(logs.AdapterFile, string(b))
  81. Binlog.EnableFuncCallDepth(true)
  82. }
  83. func initApiLog() {
  84. logPath := ApiLogPath
  85. if logPath == "" {
  86. logPath = DefaultApiLogPath
  87. }
  88. logFile := ApiLogFile
  89. if logFile == "" {
  90. logFile = "apilog.log"
  91. }
  92. os.MkdirAll(logPath, os.ModePerm)
  93. // 打开文件
  94. logFileName := path.Join(logPath, logFile)
  95. rolling(ApiLog, logFileName, LogMaxDays)
  96. // 设置日志输出JSON格式
  97. jsonFormat := new(logrus.JSONFormatter)
  98. jsonFormat.DisableHTMLEscape = true
  99. jsonFormat.TimestampFormat = HlbFormatDateTime
  100. ApiLog.SetFormatter(jsonFormat)
  101. }
  102. func initFileLogData() {
  103. logPath := LogDataPath
  104. if logPath == "" {
  105. logPath = DefaultDataPath
  106. }
  107. logFile := LogDataFile
  108. if logFile == "" {
  109. logFile = "datalog.log"
  110. }
  111. os.MkdirAll(logPath, os.ModePerm)
  112. // 打开文件
  113. logFileName := path.Join(logPath, logFile)
  114. rolling(FileLogData, logFileName, LogMaxDays)
  115. // 设置日志输出JSON格式
  116. jsonFormat := new(logrus.JSONFormatter)
  117. jsonFormat.DisableHTMLEscape = true
  118. jsonFormat.TimestampFormat = HlbFormatDateTime
  119. FileLogData.SetFormatter(jsonFormat)
  120. }
  121. func getDefaultLogConfig() logConfig {
  122. return logConfig{
  123. FileName: "",
  124. MaxLines: 0,
  125. MaxSize: 1 << 28,
  126. Daily: true,
  127. MaxDays: LogMaxDays, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
  128. Rotate: true,
  129. Level: logs.LevelTrace,
  130. //Perm: "",
  131. }
  132. }
  133. // 日志滚动设置
  134. func rolling(fLog *logrus.Logger, logFileName string, maxAge int) {
  135. // 设置输出
  136. if maxAge == 0 {
  137. maxAge = LogMaxDays
  138. }
  139. content, err := rotatelogs.New(
  140. logFileName+"-%Y%m%d",
  141. //rotatelogs.WithLinkName("./log/cli.log"), // 生成软链,指向最新日志文件
  142. rotatelogs.WithClock(rotatelogs.Local),
  143. //MaxAge and RotationCount cannot be both set 两者不能同时设置
  144. rotatelogs.WithRotationSize(1<<28), //256mb
  145. rotatelogs.WithMaxAge(time.Duration(maxAge*24)*time.Hour), //clear
  146. //rotatelogs.WithRotationCount(5), //number 默认7份 大于7份 或到了清理时间 开始清理
  147. rotatelogs.WithRotationTime(24*time.Hour), //rotate 最小为1分钟轮询。默认60s 低于1分钟就按1分钟来
  148. )
  149. if err != nil {
  150. panic("failed to create rotatelogs: " + err.Error())
  151. }
  152. fLog.SetOutput(content)
  153. return
  154. }