logs.go 4.8 KB

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