[v3] improve logs (#166)

* improve logs
This commit is contained in:
2025-10-17 13:08:02 +05:00
committed by GitHub
parent a6dd9c0455
commit 905398dcb6
3 changed files with 100 additions and 10 deletions

View File

@@ -119,10 +119,23 @@ func buildHTTPRequest(
}
if log.V(logger.DebugLevel) {
log.Debug(
ctx,
fmt.Sprintf("request %s to %s with headers %v body %s", method, u.String(), hreq.Header, body),
)
if shouldLogBody(ct) {
log.Debug(
ctx,
fmt.Sprintf(
"micro.client http request: method=%s url=%s headers=%v body=%s",
method, u.String(), hreq.Header, body,
),
)
} else {
log.Debug(
ctx,
fmt.Sprintf(
"micro.client http request: method=%s url=%s headers=%v",
method, u.String(), hreq.Header,
),
)
}
}
return hreq, nil
@@ -259,3 +272,18 @@ func validateHeadersAndCookies(r *http.Request, parameters map[string]map[string
return nil
}
func shouldLogBody(contentType string) bool {
ct := strings.ToLower(strings.Split(contentType, ";")[0])
switch {
case strings.HasPrefix(ct, "text/"): // => text/html, text/plain, text/csv etc.
return true
case ct == "application/json",
ct == "application/xml",
ct == "application/x-www-form-urlencoded",
ct == "application/yaml":
return true
default:
return false
}
}