Modify trace logging for more info
All checks were successful
/ docker (push) Successful in 4m19s

This commit is contained in:
Melody Becker 2025-05-12 15:27:08 +02:00
parent 96f03d4659
commit 26cabc02f6
Signed by: mstar
SSH key fingerprint: SHA256:vkXfS9FG2pVNVfvDrzd1VW9n8VJzqqdKQGljxxX8uK8

View file

@ -14,15 +14,27 @@ func TraceRequestInfoMiddleware(h http.Handler) http.Handler {
return webutils.ChainMiddlewares(
h,
hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
if !hlog.FromRequest(r).Trace().Enabled() {
return
}
body, _ := io.ReadAll(r.Body)
r.Body = io.NopCloser(bytes.NewReader(body))
IPAddress := r.Header.Get("X-Real-Ip")
if IPAddress == "" {
IPAddress = r.Header.Get("X-Forwarded-For")
}
if IPAddress == "" {
IPAddress = r.RemoteAddr
}
hlog.FromRequest(r).Trace().Any("headers", r.Header).
Bytes("body", body).
Str("method", r.Method).
Stringer("url", r.URL).
Int("status", status).
Int("size", size).
Dur("duration", duration)
Str("real-ip", IPAddress).
Dur("duration", duration).
Send()
}),
)
}