logs.go 4.2 KB

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