[v4] improve logs (#165)

* improve logs
This commit is contained in:
2025-10-17 12:56:06 +05:00
committed by GitHub
parent 62bce65656
commit 60979b12eb
3 changed files with 100 additions and 10 deletions

View File

@@ -207,8 +207,6 @@ func (c *Client) parseRsp(ctx context.Context, hrsp *http.Response, rsp any, opt
default:
}
var buf []byte
if opts.ResponseMetadata != nil {
for k, v := range hrsp.Header {
opts.ResponseMetadata.Append(k, v...)
@@ -224,6 +222,8 @@ func (c *Client) parseRsp(ctx context.Context, hrsp *http.Response, rsp any, opt
ct = htype
}
var buf []byte
if hrsp.Body != nil {
var err error
buf, err = io.ReadAll(hrsp.Body)
@@ -232,15 +232,31 @@ func (c *Client) parseRsp(ctx context.Context, hrsp *http.Response, rsp any, opt
}
}
if log.V(logger.DebugLevel) {
if shouldLogBody(ct) {
log.Debug(
ctx,
fmt.Sprintf(
"micro.client http response: status=%s headers=%v body=%s",
hrsp.Status, hrsp.Header, buf,
),
)
} else {
log.Debug(
ctx,
fmt.Sprintf(
"micro.client http response: status=%s headers=%v",
hrsp.Status, hrsp.Header,
),
)
}
}
cf, err := c.newCodec(ct)
if err != nil {
return errors.InternalServerError("go.micro.client", "unknown content-type %s: %v", ct, err)
}
if log.V(logger.DebugLevel) {
log.Debug(ctx, fmt.Sprintf("response with headers: %v and body: %s", hrsp.Header, buf))
}
if hrsp.StatusCode < http.StatusBadRequest {
if err = cf.Unmarshal(buf, rsp); err != nil {
return errors.InternalServerError("go.micro.client", "unmarshal response: %v", err)