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.
46 lines
723 B
46 lines
723 B
4 years ago
|
package middleware
|
||
|
|
||
|
import (
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"github.com/taoshihan1991/imaptool/tools"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func NewMidLogger() gin.HandlerFunc {
|
||
|
logger := tools.Logger()
|
||
|
return func(c *gin.Context) {
|
||
|
// 开始时间
|
||
|
startTime := time.Now()
|
||
|
|
||
|
// 处理请求
|
||
|
c.Next()
|
||
|
|
||
|
// 结束时间
|
||
|
endTime := time.Now()
|
||
|
|
||
|
// 执行时间
|
||
|
latencyTime := endTime.Sub(startTime)
|
||
|
|
||
|
// 请求方式
|
||
|
reqMethod := c.Request.Method
|
||
|
|
||
|
// 请求路由
|
||
|
reqUri := c.Request.RequestURI
|
||
|
|
||
|
// 状态码
|
||
|
statusCode := c.Writer.Status()
|
||
|
|
||
|
// 请求IP
|
||
|
clientIP := c.ClientIP()
|
||
|
|
||
|
//日志格式
|
||
|
logger.Infof("| %3d | %13v | %15s | %s | %s |",
|
||
|
statusCode,
|
||
|
latencyTime,
|
||
|
clientIP,
|
||
|
reqMethod,
|
||
|
reqUri,
|
||
|
)
|
||
|
}
|
||
|
}
|