logger.go 651 B

12345678910111213141516171819202122232425262728293031323334
  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. }