Add middleware to include the request ID in all logs.
This commit is contained in:
parent
4096691df0
commit
26e456eae3
5 changed files with 28 additions and 10 deletions
19
middleware/log.go
Normal file
19
middleware/log.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue