Include the request ID in the request logs
This commit is contained in:
parent
2968ea3498
commit
464f26a2c9
|
@ -152,8 +152,9 @@ func (re *RequestError) asFilesystemError() (int, string) {
|
||||||
func AttachRequestID() gin.HandlerFunc {
|
func AttachRequestID() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
id := uuid.New().String()
|
id := uuid.New().String()
|
||||||
c.Header("X-Request-Id", id)
|
c.Set("request_id", id)
|
||||||
c.Set("logger", log.WithField("request_id", id))
|
c.Set("logger", log.WithField("request_id", id))
|
||||||
|
c.Header("X-Request-Id", id)
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -311,4 +312,4 @@ func ExtractServer(c *gin.Context) *server.Server {
|
||||||
panic("middleware/middleware: cannot extract server: not present in request context")
|
panic("middleware/middleware: cannot extract server: not present in request context")
|
||||||
}
|
}
|
||||||
return v.(*server.Server)
|
return v.(*server.Server)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
// 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
|
// this output in production and still get meaningful logs from it since they'll likely just be a huge
|
||||||
// spamfest.
|
// spamfest.
|
||||||
|
router.Use()
|
||||||
router.Use(gin.LoggerWithFormatter(func(params gin.LogFormatterParams) string {
|
router.Use(gin.LoggerWithFormatter(func(params gin.LogFormatterParams) string {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"client_ip": params.ClientIP,
|
"client_ip": params.ClientIP,
|
||||||
"status": params.StatusCode,
|
"status": params.StatusCode,
|
||||||
"latency": params.Latency,
|
"latency": params.Latency,
|
||||||
|
"request_id": params.Keys["request_id"],
|
||||||
}).Debugf("%s %s", params.MethodColor()+params.Method+params.ResetColor(), params.Path)
|
}).Debugf("%s %s", params.MethodColor()+params.Method+params.ResetColor(), params.Path)
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user