38 lines
657 B
Go
38 lines
657 B
Go
package middleware
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Logger 日志中间件
|
|
func Logger() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
start := time.Now()
|
|
path := c.Request.URL.Path
|
|
raw := c.Request.URL.RawQuery
|
|
|
|
c.Next()
|
|
|
|
latency := time.Since(start)
|
|
clientIP := c.ClientIP()
|
|
method := c.Request.Method
|
|
statusCode := c.Writer.Status()
|
|
|
|
if raw != "" {
|
|
path = path + "?" + raw
|
|
}
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
"status_code": statusCode,
|
|
"latency": latency,
|
|
"client_ip": clientIP,
|
|
"method": method,
|
|
"path": path,
|
|
}).Info("HTTP Request")
|
|
}
|
|
}
|
|
|