logs.go 4.9 KB

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