From 836793116bee2880a228166a8d3f4d968497c9f1 Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Mon, 12 Feb 2024 12:05:02 +0100 Subject: [PATCH] fix: dedup "headers" attribute --- middleware.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/middleware.go b/middleware.go index 973f651..747af9a 100644 --- a/middleware.go +++ b/middleware.go @@ -192,12 +192,16 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc { // request headers if config.WithRequestHeader { + kv := []any{} + for k, v := range c.Request().Header { if _, found := HiddenRequestHeaders[strings.ToLower(k)]; found { continue } - requestAttributes = append(requestAttributes, slog.Group("header", slog.Any(k, v))) + kv = append(kv, slog.Any(k, v)) } + + responseAttributes = append(responseAttributes, slog.Group("header", kv...)) } if config.WithUserAgent { @@ -220,12 +224,16 @@ func NewWithConfig(logger *slog.Logger, config Config) echo.MiddlewareFunc { // response headers if config.WithResponseHeader { + kv := []any{} + for k, v := range c.Response().Header() { if _, found := HiddenResponseHeaders[strings.ToLower(k)]; found { continue } - responseAttributes = append(responseAttributes, slog.Group("header", slog.Any(k, v))) + kv = append(kv, slog.Any(k, v)) } + + responseAttributes = append(responseAttributes, slog.Group("header", kv...)) } attributes := append(