diff --git a/core/engine.go b/core/engine.go index de7948d..8d87d6b 100644 --- a/core/engine.go +++ b/core/engine.go @@ -111,11 +111,6 @@ func (e *Engine) initGin() { e.buildSentryConfig() e.InitSentrySDK() r.Use(e.SentryMiddlewares()...) - - if e.Config.IsDebug() { - r.Use(gin.Logger()) - } - r.Use(e.LocalizationMiddleware()) e.ginEngine = r } @@ -179,6 +174,8 @@ func (e *Engine) UseZabbix(collectors []metrics.Collector) *Engine { 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 { if e.Logger() == nil { return e diff --git a/core/logger/attrs.go b/core/logger/attrs.go index 009f3d6..3f21387 100644 --- a/core/logger/attrs.go +++ b/core/logger/attrs.go @@ -14,9 +14,9 @@ const ( ErrorAttr = "error" FailureMessageAttr = "failureMessage" BodyAttr = "body" - HTTPMethodAttr = "httpMethod" - HTTPStatusAttr = "httpStatusCode" - HTTPStatusNameAttr = "httpStatusName" + HTTPMethodAttr = "method" + HTTPStatusAttr = "statusCode" + HTTPStatusNameAttr = "statusName" ) func Err(err any) slog.Attr { diff --git a/core/logger/gin.go b/core/logger/gin.go new file mode 100644 index 0000000..21eb8ec --- /dev/null +++ b/core/logger/gin.go @@ -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()), + ) + } +}