logs.go 4.0 KB

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