mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-21 20:56:04 +03:00
gin middleware for logging
This commit is contained in:
parent
8fd4bc26bf
commit
f5bda28e92
@ -111,11 +111,6 @@ func (e *Engine) initGin() {
|
|||||||
e.buildSentryConfig()
|
e.buildSentryConfig()
|
||||||
e.InitSentrySDK()
|
e.InitSentrySDK()
|
||||||
r.Use(e.SentryMiddlewares()...)
|
r.Use(e.SentryMiddlewares()...)
|
||||||
|
|
||||||
if e.Config.IsDebug() {
|
|
||||||
r.Use(gin.Logger())
|
|
||||||
}
|
|
||||||
|
|
||||||
r.Use(e.LocalizationMiddleware())
|
r.Use(e.LocalizationMiddleware())
|
||||||
e.ginEngine = r
|
e.ginEngine = r
|
||||||
}
|
}
|
||||||
@ -179,6 +174,8 @@ func (e *Engine) UseZabbix(collectors []metrics.Collector) *Engine {
|
|||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HijackGinLogs will take control of GIN debug logs and will convert them into structured logs.
|
||||||
|
// It will also affect default logging middleware. Use logger.GinMiddleware to circumvent this.
|
||||||
func (e *Engine) HijackGinLogs() *Engine {
|
func (e *Engine) HijackGinLogs() *Engine {
|
||||||
if e.Logger() == nil {
|
if e.Logger() == nil {
|
||||||
return e
|
return e
|
||||||
|
@ -14,9 +14,9 @@ const (
|
|||||||
ErrorAttr = "error"
|
ErrorAttr = "error"
|
||||||
FailureMessageAttr = "failureMessage"
|
FailureMessageAttr = "failureMessage"
|
||||||
BodyAttr = "body"
|
BodyAttr = "body"
|
||||||
HTTPMethodAttr = "httpMethod"
|
HTTPMethodAttr = "method"
|
||||||
HTTPStatusAttr = "httpStatusCode"
|
HTTPStatusAttr = "statusCode"
|
||||||
HTTPStatusNameAttr = "httpStatusName"
|
HTTPStatusNameAttr = "statusName"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Err(err any) slog.Attr {
|
func Err(err any) slog.Attr {
|
||||||
|
35
core/logger/gin.go
Normal file
35
core/logger/gin.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package logger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"log/slog"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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("[GIN] request",
|
||||||
|
slog.String("startTime", start.Format(time.RFC3339)),
|
||||||
|
slog.String("endTime", end.Format(time.RFC3339)),
|
||||||
|
slog.Any("latency", end.Sub(start)/time.Millisecond),
|
||||||
|
slog.String("remoteAddress", c.ClientIP()),
|
||||||
|
slog.String(HTTPMethodAttr, c.Request.Method),
|
||||||
|
slog.String("path", path),
|
||||||
|
slog.Int("bodySize", c.Writer.Size()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user