loghook.go 849 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package middleware
  2. import (
  3. "DataShare/global"
  4. "github.com/gin-gonic/gin"
  5. "github.com/sirupsen/logrus"
  6. )
  7. func LogHook() gin.HandlerFunc {
  8. return func(c *gin.Context) {
  9. c.Next()
  10. //请求方式
  11. method:=c.Request.Method
  12. //请求路由
  13. reqUrl := c.Request.RequestURI
  14. //状态码
  15. statusCode := c.Writer.Status()
  16. //请求ip
  17. clientIP := c.ClientIP()
  18. //请求参数:
  19. params,exist := c.Get("params")
  20. if exist == false {
  21. global.SystemLogger.WithFields(logrus.Fields{
  22. "status_code": statusCode,
  23. "client_ip": clientIP,
  24. "req_method": method,
  25. "req_uri": reqUrl,
  26. }).Info()
  27. }else{
  28. global.SystemLogger.WithFields(logrus.Fields{
  29. "status_code": statusCode,
  30. "client_ip": clientIP,
  31. "req_method": method,
  32. "req_uri": reqUrl,
  33. "params": params,
  34. }).Info()
  35. }
  36. }
  37. }