logs.go 4.8 KB

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