From 11def456669012df0d3c236096de1a9edf40a98e Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Wed, 19 Jun 2024 09:32:22 -0400 Subject: [PATCH] Log request URI rather than path. --- pkg/log/log.go | 5 +++++ pkg/middleware/log.go | 8 +------- pkg/middleware/log_test.go | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/pkg/log/log.go b/pkg/log/log.go index b609815..ebafd28 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -18,5 +18,10 @@ func Ctx(ctx echo.Context) *slog.Logger { return l } + return Default() +} + +// Default returns the default logger +func Default() *slog.Logger { return slog.Default() } diff --git a/pkg/middleware/log.go b/pkg/middleware/log.go index 6964b0c..d9eede0 100644 --- a/pkg/middleware/log.go +++ b/pkg/middleware/log.go @@ -59,13 +59,7 @@ func LogRequest() echo.MiddlewareFunc { "latency", stop.Sub(start).String(), ) - msg := fmt.Sprintf("%s %s", req.Method, func() string { - p := req.URL.Path - if p == "" { - p = "/" - } - return p - }()) + msg := fmt.Sprintf("%s %s", req.Method, req.URL.RequestURI()) if res.Status >= 500 { sub.Error(msg) diff --git a/pkg/middleware/log_test.go b/pkg/middleware/log_test.go index 0549e24..129376a 100644 --- a/pkg/middleware/log_test.go +++ b/pkg/middleware/log_test.go @@ -77,7 +77,7 @@ func TestLogRequest(t *testing.T) { h := new(mockLogHandler) exec := func() { - ctx, _ := tests.NewContext(c.Web, "http://test.localhost/abc") + ctx, _ := tests.NewContext(c.Web, "http://test.localhost/abc?d=1&e=2") logger := slog.New(h).With("previous", "param") log.Set(ctx, logger) ctx.Request().Header.Set("Referer", "ref.com") @@ -101,7 +101,7 @@ func TestLogRequest(t *testing.T) { assert.Equal(t, "5", h.GetAttr("bytes_out")) assert.NotEmpty(t, h.GetAttr("latency")) assert.Equal(t, "INFO", h.level) - assert.Equal(t, "GET /abc", h.msg) + assert.Equal(t, "GET /abc?d=1&e=2", h.msg) statusCode = 500 exec()