logger.go 482 B

1234567891011121314151617181920212223242526
  1. package middleware
  2. import (
  3. "app/commons/core"
  4. "github.com/gin-gonic/gin"
  5. "time"
  6. )
  7. // 自定义日志拦截
  8. func GinLogger() gin.HandlerFunc {
  9. return func(c *gin.Context) {
  10. start := time.Now()
  11. defer func() {
  12. // 在请求结束时记录日志
  13. elapsed := time.Since(start)
  14. core.Log.Infof("GIN|%d|%s|IP:%s|%s|URI:%s|",
  15. c.Writer.Status(),
  16. elapsed.Round(time.Millisecond),
  17. c.ClientIP(),
  18. c.Request.Method,
  19. c.Request.URL.Path)
  20. }()
  21. c.Next()
  22. }
  23. }