mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-24 22:26:04 +03:00
38 lines
834 B
Go
38 lines
834 B
Go
package logger
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// GinMiddleware will construct Gin middleware which will log requests.
|
|
func GinMiddleware(log Logger) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// Start timer
|
|
start := time.Now()
|
|
path := c.Request.URL.Path
|
|
raw := c.Request.URL.RawQuery
|
|
|
|
// Process request
|
|
c.Next()
|
|
|
|
end := time.Now()
|
|
if raw != "" {
|
|
path = path + "?" + raw
|
|
}
|
|
|
|
log.Info("request",
|
|
zap.String(HandlerAttr, "GIN"),
|
|
zap.String("startTime", start.Format(time.RFC3339)),
|
|
zap.String("endTime", end.Format(time.RFC3339)),
|
|
zap.Any("latency", end.Sub(start)/time.Millisecond),
|
|
zap.String("remoteAddress", c.ClientIP()),
|
|
zap.String(HTTPMethodAttr, c.Request.Method),
|
|
zap.String("path", path),
|
|
zap.Int("bodySize", c.Writer.Size()),
|
|
)
|
|
}
|
|
}
|