You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
651 B
34 lines
651 B
package middleware
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"main/init/qmlog"
|
|
"time"
|
|
)
|
|
|
|
func Logger() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// 开始时间
|
|
start := time.Now()
|
|
// 处理请求
|
|
c.Next()
|
|
// 结束时间
|
|
end := time.Now()
|
|
//执行时间
|
|
latency := end.Sub(start)
|
|
|
|
path := c.Request.URL.Path
|
|
clientIP := c.ClientIP()
|
|
method := c.Request.Method
|
|
statusCode := c.Writer.Status()
|
|
buf := make([]byte, 1024)
|
|
n, _ := c.Request.Body.Read(buf)
|
|
requestParams := buf[0:n]
|
|
qmlog.QMLog.Infof("| %3d | %13v | %15s | %s %s |%s|",
|
|
statusCode,
|
|
latency,
|
|
clientIP,
|
|
method, path, requestParams,
|
|
)
|
|
}
|
|
}
|