Log request URI rather than path.

This commit is contained in:
mikestefanello 2024-06-19 09:32:22 -04:00
parent ca22f54c89
commit 11def45666
3 changed files with 8 additions and 9 deletions

View file

@ -18,5 +18,10 @@ func Ctx(ctx echo.Context) *slog.Logger {
return l return l
} }
return Default()
}
// Default returns the default logger
func Default() *slog.Logger {
return slog.Default() return slog.Default()
} }

View file

@ -59,13 +59,7 @@ func LogRequest() echo.MiddlewareFunc {
"latency", stop.Sub(start).String(), "latency", stop.Sub(start).String(),
) )
msg := fmt.Sprintf("%s %s", req.Method, func() string { msg := fmt.Sprintf("%s %s", req.Method, req.URL.RequestURI())
p := req.URL.Path
if p == "" {
p = "/"
}
return p
}())
if res.Status >= 500 { if res.Status >= 500 {
sub.Error(msg) sub.Error(msg)

View file

@ -77,7 +77,7 @@ func TestLogRequest(t *testing.T) {
h := new(mockLogHandler) h := new(mockLogHandler)
exec := func() { 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") logger := slog.New(h).With("previous", "param")
log.Set(ctx, logger) log.Set(ctx, logger)
ctx.Request().Header.Set("Referer", "ref.com") 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.Equal(t, "5", h.GetAttr("bytes_out"))
assert.NotEmpty(t, h.GetAttr("latency")) assert.NotEmpty(t, h.GetAttr("latency"))
assert.Equal(t, "INFO", h.level) 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 statusCode = 500
exec() exec()