Skip to content

Commit 60979b1

Browse files
authored
[v4] improve logs (#165)
* improve logs
1 parent 62bce65 commit 60979b1

File tree

3 files changed

+100
-10
lines changed

3 files changed

+100
-10
lines changed

client_helpers.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,23 @@ func buildHTTPRequest(
119119
}
120120

121121
if log.V(logger.DebugLevel) {
122-
log.Debug(
123-
ctx,
124-
fmt.Sprintf("request %s to %s with headers %v body %s", method, u.String(), hreq.Header, body),
125-
)
122+
if shouldLogBody(ct) {
123+
log.Debug(
124+
ctx,
125+
fmt.Sprintf(
126+
"micro.client http request: method=%s url=%s headers=%v body=%s",
127+
method, u.String(), hreq.Header, body,
128+
),
129+
)
130+
} else {
131+
log.Debug(
132+
ctx,
133+
fmt.Sprintf(
134+
"micro.client http request: method=%s url=%s headers=%v",
135+
method, u.String(), hreq.Header,
136+
),
137+
)
138+
}
126139
}
127140

128141
return hreq, nil
@@ -261,3 +274,18 @@ func validateHeadersAndCookies(r *http.Request, parameters map[string]map[string
261274

262275
return nil
263276
}
277+
278+
func shouldLogBody(contentType string) bool {
279+
ct := strings.ToLower(strings.Split(contentType, ";")[0])
280+
switch {
281+
case strings.HasPrefix(ct, "text/"): // => text/html, text/plain, text/csv etc.
282+
return true
283+
case ct == "application/json",
284+
ct == "application/xml",
285+
ct == "application/x-www-form-urlencoded",
286+
ct == "application/yaml":
287+
return true
288+
default:
289+
return false
290+
}
291+
}

client_helpers_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,49 @@ func TestValidateHeadersAndCookies(t *testing.T) {
399399
})
400400
}
401401
}
402+
403+
func TestShouldLogBody(t *testing.T) {
404+
tests := []struct {
405+
name string
406+
contentType string
407+
want bool
408+
}{
409+
// --- text/*
410+
{"plain text", "text/plain", true},
411+
{"html", "text/html", true},
412+
{"csv", "text/csv", true},
413+
{"yaml text", "text/yaml", true},
414+
415+
// --- application/*
416+
{"json", "application/json", true},
417+
{"xml", "application/xml", true},
418+
{"form-urlencoded", "application/x-www-form-urlencoded", true},
419+
{"yaml", "application/yaml", true},
420+
421+
// --- with parameters
422+
{"json with charset", "application/json; charset=utf-8", true},
423+
{"binary with charset", "application/octet-stream; charset=utf-8", false},
424+
425+
// --- binary
426+
{"multipart form", "multipart/form-data", false},
427+
{"binary stream", "application/octet-stream", false},
428+
{"pdf", "application/pdf", false},
429+
{"protobuf", "application/protobuf", false},
430+
{"image", "image/png", false},
431+
432+
// --- edge cases
433+
{"upper case type", "APPLICATION/JSON", true},
434+
{"mixed case type", "Text/HTML", true},
435+
{"unknown text prefix", "TEXT/FOO", true},
436+
{"weird semicolon only", ";", false},
437+
{"spaces only", " ", false},
438+
{"empty content-type", "", false},
439+
{"missing main type", "/plain", false},
440+
}
441+
442+
for _, tt := range tests {
443+
t.Run(tt.name, func(t *testing.T) {
444+
require.Equal(t, tt.want, shouldLogBody(tt.contentType))
445+
})
446+
}
447+
}

client_unary_call.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ func (c *Client) parseRsp(ctx context.Context, hrsp *http.Response, rsp any, opt
207207
default:
208208
}
209209

210-
var buf []byte
211-
212210
if opts.ResponseMetadata != nil {
213211
for k, v := range hrsp.Header {
214212
opts.ResponseMetadata.Append(k, v...)
@@ -224,6 +222,8 @@ func (c *Client) parseRsp(ctx context.Context, hrsp *http.Response, rsp any, opt
224222
ct = htype
225223
}
226224

225+
var buf []byte
226+
227227
if hrsp.Body != nil {
228228
var err error
229229
buf, err = io.ReadAll(hrsp.Body)
@@ -232,15 +232,31 @@ func (c *Client) parseRsp(ctx context.Context, hrsp *http.Response, rsp any, opt
232232
}
233233
}
234234

235+
if log.V(logger.DebugLevel) {
236+
if shouldLogBody(ct) {
237+
log.Debug(
238+
ctx,
239+
fmt.Sprintf(
240+
"micro.client http response: status=%s headers=%v body=%s",
241+
hrsp.Status, hrsp.Header, buf,
242+
),
243+
)
244+
} else {
245+
log.Debug(
246+
ctx,
247+
fmt.Sprintf(
248+
"micro.client http response: status=%s headers=%v",
249+
hrsp.Status, hrsp.Header,
250+
),
251+
)
252+
}
253+
}
254+
235255
cf, err := c.newCodec(ct)
236256
if err != nil {
237257
return errors.InternalServerError("go.micro.client", "unknown content-type %s: %v", ct, err)
238258
}
239259

240-
if log.V(logger.DebugLevel) {
241-
log.Debug(ctx, fmt.Sprintf("response with headers: %v and body: %s", hrsp.Header, buf))
242-
}
243-
244260
if hrsp.StatusCode < http.StatusBadRequest {
245261
if err = cf.Unmarshal(buf, rsp); err != nil {
246262
return errors.InternalServerError("go.micro.client", "unmarshal response: %v", err)

0 commit comments

Comments
 (0)