From 26e456eae30a78e079a92f422b27d026c4f071ae Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Wed, 8 Dec 2021 21:55:30 -0500 Subject: [PATCH] Add middleware to include the request ID in all logs. --- controllers/controller.go | 5 ++--- controllers/router.go | 1 + funcmap/funcmap.go | 1 + middleware/cache.go | 12 +++++------- middleware/log.go | 19 +++++++++++++++++++ 5 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 middleware/log.go diff --git a/controllers/controller.go b/controllers/controller.go index 4dcd068..6c1ffcd 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -99,12 +99,11 @@ func (t *Controller) cachePage(c echo.Context, p Page, html *bytes.Buffer) { } err := marshaler.New(t.Container.Cache).Set(c.Request().Context(), key, cp, opts) if err != nil { - c.Logger().Errorf("failed to cache page: %s", key) - c.Logger().Error(err) + c.Logger().Errorf("failed to cache page: %v", err) return } - c.Logger().Infof("cached page for: %s", key) + c.Logger().Infof("cached page") } func (t *Controller) parsePageTemplates(p Page) error { diff --git a/controllers/router.go b/controllers/router.go index 058a0d0..0f04559 100644 --- a/controllers/router.go +++ b/controllers/router.go @@ -35,6 +35,7 @@ func BuildRouter(c *container.Container) { echomw.Recover(), echomw.Gzip(), echomw.Logger(), + middleware.LogRequestID(), echomw.TimeoutWithConfig(echomw.TimeoutConfig{ Timeout: c.Config.App.Timeout, }), diff --git a/funcmap/funcmap.go b/funcmap/funcmap.go index 3cff8e9..3457a53 100644 --- a/funcmap/funcmap.go +++ b/funcmap/funcmap.go @@ -45,6 +45,7 @@ func HasField(v interface{}, name string) bool { // File appends a cache key to a given filepath so it can remain cached until the app is restarted func File(filepath string) string { + // TODO: Use const for path prefix return fmt.Sprintf("/files/%s?v=%s", filepath, CacheKey) } diff --git a/middleware/cache.go b/middleware/cache.go index 51f1fbf..99f3d25 100644 --- a/middleware/cache.go +++ b/middleware/cache.go @@ -20,21 +20,19 @@ type CachedPage struct { func PageCache(ch *cache.Cache) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { - key := c.Request().URL.String() - res, err := marshaler.New(ch).Get(c.Request().Context(), key, new(CachedPage)) + res, err := marshaler.New(ch).Get(c.Request().Context(), c.Request().URL.String(), new(CachedPage)) if err != nil { if err == redis.Nil { - c.Logger().Infof("no cached page for: %s", key) + c.Logger().Infof("no cached page found") } else { - c.Logger().Errorf("failed getting cached page: %s", key) - c.Logger().Error(err) + c.Logger().Errorf("failed getting cached page: %v", err) } return next(c) } page, ok := res.(*CachedPage) if !ok { - c.Logger().Errorf("failed casting cached page: %s", key) + c.Logger().Errorf("failed casting cached page") return next(c) } @@ -43,7 +41,7 @@ func PageCache(ch *cache.Cache) echo.MiddlewareFunc { c.Response().Header().Set(k, v) } } - c.Logger().Infof("serving cached page for: %s", key) + c.Logger().Infof("serving cached page") return c.HTMLBlob(page.StatusCode, page.HTML) } diff --git a/middleware/log.go b/middleware/log.go new file mode 100644 index 0000000..620709a --- /dev/null +++ b/middleware/log.go @@ -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) + } + } +}