项目初始化
This commit is contained in:
37
middleware/logger.go
Normal file
37
middleware/logger.go
Normal file
@@ -0,0 +1,37 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user