logger.go 762 B

123456789101112131415161718192021222324252627282930313233343536
  1. package middleware
  2. import (
  3. "github.com/gin-gonic/gin"
  4. core2 "go_server/base/core"
  5. "go_server/model/system"
  6. "time"
  7. )
  8. // 自定义日志拦截
  9. func GinLogger() gin.HandlerFunc {
  10. return func(c *gin.Context) {
  11. start := time.Now()
  12. defer func() {
  13. // 在请求结束时记录日志
  14. elapsed := time.Since(start)
  15. core2.Log.Infof("GIN|%d|%s|IP:%s|%s|URI:%s|",
  16. c.Writer.Status(),
  17. elapsed.Round(time.Millisecond),
  18. c.ClientIP(),
  19. c.Request.Method,
  20. c.Request.URL.Path)
  21. }()
  22. c.Next()
  23. adminId := c.GetInt64("userId")
  24. core2.MainDb().Create(&system.AdministratorLog{
  25. AdminId: adminId,
  26. Method: c.Request.Method,
  27. Path: c.Request.URL.Path,
  28. Ip: c.ClientIP(),
  29. UserAgent: c.Request.UserAgent(),
  30. })
  31. }
  32. }