This commit is contained in:
parent
96f03d4659
commit
26cabc02f6
1 changed files with 13 additions and 1 deletions
|
@ -14,15 +14,27 @@ func TraceRequestInfoMiddleware(h http.Handler) http.Handler {
|
||||||
return webutils.ChainMiddlewares(
|
return webutils.ChainMiddlewares(
|
||||||
h,
|
h,
|
||||||
hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
|
hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
|
||||||
|
if !hlog.FromRequest(r).Trace().Enabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
body, _ := io.ReadAll(r.Body)
|
body, _ := io.ReadAll(r.Body)
|
||||||
r.Body = io.NopCloser(bytes.NewReader(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).
|
hlog.FromRequest(r).Trace().Any("headers", r.Header).
|
||||||
Bytes("body", body).
|
Bytes("body", body).
|
||||||
Str("method", r.Method).
|
Str("method", r.Method).
|
||||||
Stringer("url", r.URL).
|
Stringer("url", r.URL).
|
||||||
Int("status", status).
|
Int("status", status).
|
||||||
Int("size", size).
|
Int("size", size).
|
||||||
Dur("duration", duration)
|
Str("real-ip", IPAddress).
|
||||||
|
Dur("duration", duration).
|
||||||
|
Send()
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue