ferry/middleware/logger.go

49 lines
759 B
Go
Raw Normal View History

2020-07-13 20:33:20 +08:00
package middleware
import (
"time"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
// 日志记录到文件
func LoggerToFile() gin.HandlerFunc {
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()
// 日志格式
log.Infof(" %s %3d %13v %15s %s %s \r\n",
startTime.Format("2006-01-02 15:04:05.9999"),
statusCode,
latencyTime,
clientIP,
reqMethod,
reqUri,
)
}
}