diff --git a/router/middleware/middleware.go b/router/middleware/middleware.go index b63d49f..ff61300 100644 --- a/router/middleware/middleware.go +++ b/router/middleware/middleware.go @@ -152,8 +152,9 @@ func (re *RequestError) asFilesystemError() (int, string) { func AttachRequestID() gin.HandlerFunc { return func(c *gin.Context) { id := uuid.New().String() - c.Header("X-Request-Id", id) + c.Set("request_id", id) c.Set("logger", log.WithField("request_id", id)) + c.Header("X-Request-Id", id) c.Next() } } @@ -311,4 +312,4 @@ func ExtractServer(c *gin.Context) *server.Server { panic("middleware/middleware: cannot extract server: not present in request context") } return v.(*server.Server) -} \ No newline at end of file +} diff --git a/router/router.go b/router/router.go index cce9f71..947a504 100644 --- a/router/router.go +++ b/router/router.go @@ -18,11 +18,13 @@ func Configure() *gin.Engine { // lifecycle and quickly seeing what was called leading to the logs. However, it isn't feasible to mix // this output in production and still get meaningful logs from it since they'll likely just be a huge // spamfest. + router.Use() router.Use(gin.LoggerWithFormatter(func(params gin.LogFormatterParams) string { log.WithFields(log.Fields{ - "client_ip": params.ClientIP, - "status": params.StatusCode, - "latency": params.Latency, + "client_ip": params.ClientIP, + "status": params.StatusCode, + "latency": params.Latency, + "request_id": params.Keys["request_id"], }).Debugf("%s %s", params.MethodColor()+params.Method+params.ResetColor(), params.Path) return ""