Misc cleanup.
This commit is contained in:
parent
9acf73a4d9
commit
6a7070a729
@ -1250,7 +1250,7 @@ The `SetLogger()` middleware has been added to the router which sets an initiali
|
|||||||
The `LogRequest()` middleware is a replacement for Echo's `Logger()` middleware which produces a log of every request made, but uses our logger rather than Echo's.
|
The `LogRequest()` middleware is a replacement for Echo's `Logger()` middleware which produces a log of every request made, but uses our logger rather than Echo's.
|
||||||
|
|
||||||
```
|
```
|
||||||
2024/06/14 19:44:16 INFO request_id=snoonQoyRSEBcQthnIzIWIeVRxmutyAV ip=::1 host=localhost:8000 method=GET path=/about referer=http://localhost:8000/ status=200 bytes_in=0 bytes_out=6695 latency=18.022502ms
|
2024/06/15 09:07:11 INFO GET /contact request_id=gNblvugTKcyLnBYPMPTwMPEqDOioVLKp ip=::1 host=localhost:8000 referer="" status=200 bytes_in=0 bytes_out=5925 latency=107.527803ms
|
||||||
```
|
```
|
||||||
|
|
||||||
## Roadmap
|
## Roadmap
|
||||||
|
@ -7,7 +7,7 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "127.0.0.1:6379:6379"
|
- "127.0.0.1:6379:6379"
|
||||||
db:
|
db:
|
||||||
# PG 16 is currenly not supported https://github.com/ent/ent/issues/3750
|
# PG 16 is currently not supported https://github.com/ent/ent/issues/3750
|
||||||
image: postgres:15-alpine
|
image: postgres:15-alpine
|
||||||
container_name: pagoda_db
|
container_name: pagoda_db
|
||||||
ports:
|
ports:
|
||||||
|
@ -137,7 +137,9 @@ func (c *Controller) cachePage(ctx echo.Context, page Page, html *bytes.Buffer)
|
|||||||
case err == nil:
|
case err == nil:
|
||||||
log.Ctx(ctx).Debug("cached page")
|
log.Ctx(ctx).Debug("cached page")
|
||||||
case !context.IsCanceledError(err):
|
case !context.IsCanceledError(err):
|
||||||
log.Ctx(ctx).Error("failed to cache page", "error", err)
|
log.Ctx(ctx).Error("failed to cache page",
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,8 +98,12 @@ type Page struct {
|
|||||||
// This will only be populated if the request ID middleware is in effect for the given request.
|
// This will only be populated if the request ID middleware is in effect for the given request.
|
||||||
RequestID string
|
RequestID string
|
||||||
|
|
||||||
|
// HTMX provides the ability to interact with the HTMX library
|
||||||
HTMX struct {
|
HTMX struct {
|
||||||
|
// Request contains the information provided by HTMX about the current request
|
||||||
Request htmx.Request
|
Request htmx.Request
|
||||||
|
|
||||||
|
// Response contains values to pass back to HTMX
|
||||||
Response *htmx.Response
|
Response *htmx.Response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -45,14 +46,6 @@ func LogRequest() echo.MiddlewareFunc {
|
|||||||
sub := log.Ctx(ctx).With(
|
sub := log.Ctx(ctx).With(
|
||||||
"ip", ctx.RealIP(),
|
"ip", ctx.RealIP(),
|
||||||
"host", req.Host,
|
"host", req.Host,
|
||||||
"method", req.Method,
|
|
||||||
"path", func() string {
|
|
||||||
p := req.URL.Path
|
|
||||||
if p == "" {
|
|
||||||
p = "/"
|
|
||||||
}
|
|
||||||
return p
|
|
||||||
}(),
|
|
||||||
"referer", req.Referer(),
|
"referer", req.Referer(),
|
||||||
"status", res.Status,
|
"status", res.Status,
|
||||||
"bytes_in", func() string {
|
"bytes_in", func() string {
|
||||||
@ -66,12 +59,18 @@ func LogRequest() echo.MiddlewareFunc {
|
|||||||
"latency", stop.Sub(start).String(),
|
"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 {
|
if res.Status >= 500 {
|
||||||
sub.Error("")
|
sub.Error(msg)
|
||||||
} else {
|
} else {
|
||||||
sub.Info("")
|
sub.Info(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -3,7 +3,6 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
@ -96,14 +95,13 @@ func TestLogRequest(t *testing.T) {
|
|||||||
assert.Equal(t, "param", h.GetAttr("previous"))
|
assert.Equal(t, "param", h.GetAttr("previous"))
|
||||||
assert.Equal(t, "21.12.12.21", h.GetAttr("ip"))
|
assert.Equal(t, "21.12.12.21", h.GetAttr("ip"))
|
||||||
assert.Equal(t, "test.localhost", h.GetAttr("host"))
|
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, "ref.com", h.GetAttr("referer"))
|
||||||
assert.Equal(t, "200", h.GetAttr("status"))
|
assert.Equal(t, "200", h.GetAttr("status"))
|
||||||
assert.Equal(t, "0", h.GetAttr("bytes_in"))
|
assert.Equal(t, "0", h.GetAttr("bytes_in"))
|
||||||
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)
|
||||||
|
|
||||||
statusCode = 500
|
statusCode = 500
|
||||||
exec()
|
exec()
|
||||||
|
Loading…
Reference in New Issue
Block a user