logs.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package utils
  2. import (
  3. "encoding/json"
  4. "github.com/beego/beego/v2/core/logs"
  5. "github.com/sirupsen/logrus"
  6. "gopkg.in/natefinch/lumberjack.v2"
  7. "os"
  8. "path"
  9. )
  10. const (
  11. DefaultLogPath = "./etalogs/filelog"
  12. DefaultBinlogPath = "./etalogs/binlog"
  13. DefaultDataPath = "./etalogs/datalog"
  14. DefaultApiLogPath = "./etalogs/apilog"
  15. )
  16. var FileLog = logrus.New()
  17. var ApiLog = logrus.New()
  18. var FileLogData = logrus.New()
  19. var Binlog *logs.BeeLogger
  20. func init() {
  21. if LogMaxDays == 0 {
  22. LogMaxDays = 30
  23. }
  24. logPath := LogPath
  25. if logPath == "" {
  26. logPath = DefaultLogPath
  27. }
  28. logFile := LogFile
  29. if logFile == "" {
  30. logFile = "filelog.log"
  31. }
  32. os.MkdirAll(logPath, os.ModePerm)
  33. // 打开文件
  34. logFileName := path.Join(logPath, logFile)
  35. logConf := getDefaultLogrusConfig(logFileName)
  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 = "binlog.log"
  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 = "apilog.log"
  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 = "datalog.log"
  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: 0, // 最大保留过期文件个数,0表示默认保留
  139. MaxAge: LogMaxDays, // 保留过期文件的最大时间间隔,单位是天
  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: LogMaxDays, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
  152. Rotate: true,
  153. Level: logs.LevelTrace,
  154. //Perm: "",
  155. }
  156. }