From 9fc6cdd07632a95d7136e4f82dbac10809dd8e98 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sat, 8 Mar 2025 14:47:21 +0100 Subject: [PATCH] feat: log request headers when debug logs are enabled Related to https://github.com/go-vikunja/vikunja/issues/415 --- pkg/routes/routes.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index 20c35c1c2..923f07539 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -52,6 +52,7 @@ package routes import ( + "encoding/json" "errors" "net/url" "strings" @@ -106,6 +107,31 @@ func NewEcho() *echo.Echo { Format: log.WebFmt + "\n", Output: log.GetLogWriter(config.LogHTTP.GetString(), "http"), })) + + if config.LogLevel.GetString() == "debug" { + e.Use(func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + headers := make(map[string][]string) + + for k, v := range c.Request().Header { + if k == "Authorization" { + continue + } + + headers[k] = v + } + + headersJSON, err := json.Marshal(headers) + if err != nil { + log.Errorf("Failed to marshal headers to JSON: %s", err.Error()) + } else { + log.Debugf("Headers: %s", headersJSON) + } + + return next(c) + } + }) + } } // panic recover