logs.go 4.9 KB

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