log_2_file.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package middleware
  2. import (
  3. "confrontation-training/common"
  4. "confrontation-training/global"
  5. "github.com/gin-gonic/gin"
  6. rotatelogs "github.com/lestrrat-go/file-rotatelogs"
  7. "github.com/rifflock/lfshook"
  8. "github.com/sirupsen/logrus"
  9. "os"
  10. "path"
  11. "time"
  12. )
  13. // LoggerToFile 日志记录到文件
  14. func LoggerToFile() gin.HandlerFunc {
  15. logFilePath := global.Config.Log2File.FilePath + common.NowTime("2006-01-02")
  16. logFileName := global.Config.Log2File.FileName + global.Config.Log2File.FileSuffix
  17. // 日志文件
  18. fileName := path.Join(logFilePath, logFileName)
  19. // 写入文件
  20. err := os.MkdirAll(logFilePath, os.ModePerm)
  21. if err != nil {
  22. panic(err)
  23. return nil
  24. }
  25. //src, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModePerm)
  26. //if err != nil {
  27. // fmt.Println("err", err)
  28. // panic(err)
  29. //}
  30. src, _ := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
  31. // 实例化
  32. logger := logrus.New()
  33. // 设置输出
  34. logger.Out = src
  35. // 设置日志级别
  36. logger.SetLevel(logrus.DebugLevel)
  37. logger.SetFormatter(&logrus.JSONFormatter{TimestampFormat: "2006-01-02 15:04:05"})
  38. // 设置 rotatelogs
  39. logWriter, err := rotatelogs.New(
  40. // 分割后的文件名称
  41. fileName+".%Y%m%d.log",
  42. // 生成软链,指向最新日志文件
  43. rotatelogs.WithLinkName(fileName),
  44. // 设置最大保存时间(7天)
  45. rotatelogs.WithMaxAge(7*24*time.Hour),
  46. // 设置日志切割时间间隔(1天)
  47. rotatelogs.WithRotationTime(24*time.Hour),
  48. )
  49. writeMap := lfshook.WriterMap{
  50. logrus.InfoLevel: logWriter,
  51. logrus.FatalLevel: logWriter,
  52. logrus.DebugLevel: logWriter,
  53. logrus.WarnLevel: logWriter,
  54. logrus.ErrorLevel: logWriter,
  55. logrus.PanicLevel: logWriter,
  56. }
  57. lfHook := lfshook.NewHook(writeMap, &logrus.JSONFormatter{
  58. TimestampFormat: "2006-01-02 15:04:05",
  59. })
  60. // 新增 Hook
  61. logger.AddHook(lfHook)
  62. return func(c *gin.Context) {
  63. // 开始时间
  64. startTime := time.Now()
  65. // 处理请求
  66. c.Next()
  67. // 结束时间
  68. endTime := time.Now()
  69. // 执行时间
  70. latencyTime := endTime.Sub(startTime)
  71. // 请求方式
  72. reqMethod := c.Request.Method
  73. // 请求路由
  74. reqUri := c.Request.RequestURI
  75. // 状态码
  76. statusCode := c.Writer.Status()
  77. // 请求IP
  78. //clientIP := c.ClientIP()
  79. clientIP := c.RemoteIP()
  80. //请求参数
  81. requestParam := c.Params
  82. // 日志格式
  83. logger.WithFields(logrus.Fields{
  84. "status_code": statusCode,
  85. "latency_time": latencyTime,
  86. "client_ip": clientIP,
  87. "req_method": reqMethod,
  88. "req_uri": reqUri,
  89. "req_param": requestParam,
  90. }).Info()
  91. }
  92. }
  93. // LoggerToMongo 日志记录到 MongoDB
  94. func LoggerToMongo() gin.HandlerFunc {
  95. return func(c *gin.Context) {
  96. }
  97. }
  98. // LoggerToES 日志记录到 ES
  99. func LoggerToES() gin.HandlerFunc {
  100. return func(c *gin.Context) {
  101. }
  102. }
  103. // LoggerToMQ 日志记录到 MQ
  104. func LoggerToMQ() gin.HandlerFunc {
  105. return func(c *gin.Context) {
  106. }
  107. }