logs.go 3.8 KB

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