logs.go 3.4 KB

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