From f5bda28e92d34fc8ec97029c263ffa4a6fef1c36 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Wed, 18 Oct 2023 18:16:59 +0300 Subject: [PATCH] gin middleware for logging --- core/engine.go | 7 ++----- core/logger/attrs.go | 6 +++--- core/logger/gin.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 core/logger/gin.go 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()), + ) + } +}