Include the request ID in the request logs

This commit is contained in:
Dane Everitt 2021-01-16 12:07:31 -08:00
parent 2968ea3498
commit 464f26a2c9
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 8 additions and 5 deletions

View File

@ -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()
} }
} }

View File

@ -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 ""