middlelogger.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package middleware
  2. import (
  3. "designs/global"
  4. "time"
  5. "github.com/gin-gonic/gin"
  6. )
  7. func Logmiddleware() gin.HandlerFunc {
  8. return func(c *gin.Context) {
  9. start := time.Now()
  10. path := c.Request.URL.Path
  11. raw := c.Request.URL.RawQuery
  12. if raw != "" {
  13. path = path + "?" + raw
  14. }
  15. c.Next() //执行其他中间件
  16. //end := time.Now()
  17. //timesub := end.Sub(start) //响应所需时间
  18. ClientIp := c.ClientIP() //客户端ip
  19. statuscode := c.Writer.Status()
  20. //var statusColor string
  21. //switch c.Writer.Status() {
  22. //case 200:
  23. // statusColor = fmt.Sprintf("\033[%dm%d\033[0m", status200, statuscode)
  24. //case 404:
  25. // statusColor = fmt.Sprintf("\033[%dm%d\033[0m", status404, statuscode)
  26. //default:
  27. // statusColor = fmt.Sprintf("\033[%dm%d\033[0m", status500, statuscode)
  28. //}
  29. //
  30. //var methodColor string
  31. //switch c.Request.Method {
  32. //case "GET":
  33. // methodColor = fmt.Sprintf("\033[%dm%s\033[0m", methodGET, c.Request.Method)
  34. //}
  35. global.Log.Infof("[GIN] %s |%s |%d |%s |%s",
  36. start.Format("2006-01-02 15:04:06"),
  37. ClientIp,
  38. statuscode,
  39. c.Request.Method,
  40. path)
  41. }
  42. }