logs.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. )
  14. var FileLog = logrus.New()
  15. var Binlog *logs.BeeLogger
  16. func init() {
  17. if LogMaxDays == 0 {
  18. LogMaxDays = 30
  19. }
  20. logPath := LogPath
  21. if logPath == "" {
  22. logPath = DefaultLogPath
  23. }
  24. logFile := LogFile
  25. if logFile == "" {
  26. logFile = "filelog.log"
  27. }
  28. os.MkdirAll(logPath, os.ModePerm)
  29. // 打开文件
  30. logFileName := path.Join(logPath, logFile)
  31. logConf := getDefaultLogrusConfig(logFileName)
  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. //FileLog.Info("abc")
  45. initBinlog()
  46. }
  47. type logConfig struct {
  48. FileName string `json:"filename" description:"保存的文件名"`
  49. MaxLines int `json:"maxlines" description:"每个文件保存的最大行数,默认值 1000000"`
  50. MaxSize int `json:"maxsize" description:"每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB"`
  51. Daily bool `json:"daily" description:"是否按照每天 logrotate,默认是 true"`
  52. MaxDays int `json:"maxdays" description:"文件最多保存多少天,默认保存 7 天"`
  53. Rotate bool `json:"rotate" description:"是否开启 logrotate,默认是 true"`
  54. Level int `json:"level" description:"日志保存的时候的级别,默认是 Trace 级别"`
  55. Color bool `json:"color" description:"日志是否输出颜色"`
  56. //Perm string `json:"perm" description:"日志文件权限"`
  57. }
  58. func initBinlog() {
  59. //binlog日志
  60. binlogPath := BinLogPath
  61. if binlogPath == "" {
  62. binlogPath = DefaultBinlogPath
  63. }
  64. binlogFile := BinLogFile
  65. if binlogFile == "" {
  66. binlogFile = "binlog.log"
  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. // 日志滚动设置
  80. func rolling(fLog *logrus.Logger, config *lumberjack.Logger) {
  81. // 设置输出
  82. fLog.SetOutput(config)
  83. }
  84. func getDefaultLogrusConfig(logFile string) (config *lumberjack.Logger) {
  85. config = &lumberjack.Logger{
  86. Filename: logFile, //日志文件位置
  87. MaxSize: 256, // 单文件最大容量,单位是MB
  88. MaxBackups: 0, // 最大保留过期文件个数
  89. MaxAge: LogMaxDays, // 保留过期文件的最大时间间隔,单位是天
  90. Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩
  91. LocalTime: true,
  92. }
  93. return
  94. }
  95. func getDefaultLogConfig() logConfig {
  96. return logConfig{
  97. FileName: "",
  98. MaxLines: 0,
  99. MaxSize: 1 << 28,
  100. Daily: true,
  101. MaxDays: LogMaxDays, //我就是喜欢31天,咋滴,不喜欢你就自己改-_-!
  102. Rotate: true,
  103. Level: logs.LevelTrace,
  104. //Perm: "",
  105. }
  106. }