You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
651 B

5 years ago
5 years ago
  1. package middleware
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "main/init/qmlog"
  5. "time"
  6. )
  7. func Logger() gin.HandlerFunc {
  8. return func(c *gin.Context) {
  9. // 开始时间
  10. start := time.Now()
  11. // 处理请求
  12. c.Next()
  13. // 结束时间
  14. end := time.Now()
  15. //执行时间
  16. latency := end.Sub(start)
  17. path := c.Request.URL.Path
  18. clientIP := c.ClientIP()
  19. method := c.Request.Method
  20. statusCode := c.Writer.Status()
  21. buf := make([]byte, 1024)
  22. n, _ := c.Request.Body.Read(buf)
  23. requestParams := buf[0:n]
  24. qmlog.QMLog.Infof("| %3d | %13v | %15s | %s %s |%s|",
  25. statusCode,
  26. latency,
  27. clientIP,
  28. method, path, requestParams,
  29. )
  30. }
  31. }