123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package middleware
- import (
- "designs/global"
- "time"
- "github.com/gin-gonic/gin"
- )
- func Logmiddleware() gin.HandlerFunc {
- return func(c *gin.Context) {
- start := time.Now()
- path := c.Request.URL.Path
- raw := c.Request.URL.RawQuery
- if raw != "" {
- path = path + "?" + raw
- }
- c.Next() //执行其他中间件
- //end := time.Now()
- //timesub := end.Sub(start) //响应所需时间
- ClientIp := c.ClientIP() //客户端ip
- statuscode := c.Writer.Status()
- //var statusColor string
- //switch c.Writer.Status() {
- //case 200:
- // statusColor = fmt.Sprintf("\033[%dm%d\033[0m", status200, statuscode)
- //case 404:
- // statusColor = fmt.Sprintf("\033[%dm%d\033[0m", status404, statuscode)
- //default:
- // statusColor = fmt.Sprintf("\033[%dm%d\033[0m", status500, statuscode)
- //}
- //
- //var methodColor string
- //switch c.Request.Method {
- //case "GET":
- // methodColor = fmt.Sprintf("\033[%dm%s\033[0m", methodGET, c.Request.Method)
- //}
- global.Log.Infof("[GIN] %s |%s |%d |%s |%s",
- start.Format("2006-01-02 15:04:06"),
- ClientIp,
- statuscode,
- c.Request.Method,
- path)
- }
- }
|