Misc cleanup.

This commit is contained in:
mikestefanello 2024-06-15 09:09:36 -04:00
parent c8a3d64918
commit baa391fb20
6 changed files with 21 additions and 18 deletions

View file

@ -1,6 +1,7 @@
package middleware
import (
"fmt"
"strconv"
"time"
@ -45,14 +46,6 @@ func LogRequest() echo.MiddlewareFunc {
sub := log.Ctx(ctx).With(
"ip", ctx.RealIP(),
"host", req.Host,
"method", req.Method,
"path", func() string {
p := req.URL.Path
if p == "" {
p = "/"
}
return p
}(),
"referer", req.Referer(),
"status", res.Status,
"bytes_in", func() string {
@ -66,12 +59,18 @@ func LogRequest() echo.MiddlewareFunc {
"latency", stop.Sub(start).String(),
)
// TODO is there a (better) way to log without a message?
msg := fmt.Sprintf("%s %s", req.Method, func() string {
p := req.URL.Path
if p == "" {
p = "/"
}
return p
}())
if res.Status >= 500 {
sub.Error("")
sub.Error(msg)
} else {
sub.Info("")
sub.Info(msg)
}
return nil

View file

@ -3,7 +3,6 @@ package middleware
import (
"context"
"log/slog"
"net/http"
"testing"
"github.com/labstack/echo/v4"
@ -96,14 +95,13 @@ func TestLogRequest(t *testing.T) {
assert.Equal(t, "param", h.GetAttr("previous"))
assert.Equal(t, "21.12.12.21", h.GetAttr("ip"))
assert.Equal(t, "test.localhost", h.GetAttr("host"))
assert.Equal(t, "/abc", h.GetAttr("path"))
assert.Equal(t, http.MethodGet, h.GetAttr("method"))
assert.Equal(t, "ref.com", h.GetAttr("referer"))
assert.Equal(t, "200", h.GetAttr("status"))
assert.Equal(t, "0", h.GetAttr("bytes_in"))
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)
statusCode = 500
exec()