logs.go 4.1 KB

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