Add middleware to include the request ID in all logs.

This commit is contained in:
mikestefanello 2021-12-08 21:55:30 -05:00
parent 4096691df0
commit 26e456eae3
5 changed files with 28 additions and 10 deletions

19
middleware/log.go Normal file
View file

@ -0,0 +1,19 @@
package middleware
import (
"fmt"
"github.com/labstack/echo/v4"
)
// LogRequestID includes the request ID in all logs for the given request
func LogRequestID() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
rid := c.Response().Header().Get(echo.HeaderXRequestID)
format := fmt.Sprintf(`{"time":"${time_rfc3339_nano}","id":"%s","level":"${level}","prefix":"${prefix}","file":"${short_file}","line":"${line}"}`, rid)
c.Logger().SetHeader(format)
return next(c)
}
}
}