diff --git a/client.go b/client.go index 66cba144..07d0b381 100644 --- a/client.go +++ b/client.go @@ -264,30 +264,32 @@ func (cli *Client) RequestStart(req *http.Request) { } func (cli *Client) LogRequestDone(req *http.Request, resp *http.Response, handlerErr error, contentLength int, duration time.Duration) { - if cli.ResponseHook != nil { - cli.ResponseHook(req, resp, duration) - } - mime := resp.Header.Get("Content-Type") - length := resp.ContentLength - if length == -1 && contentLength > 0 { - length = int64(contentLength) - } evt := zerolog.Ctx(req.Context()).Debug(). Str("method", req.Method). Str("url", req.URL.String()). - Int("status_code", resp.StatusCode). - Int64("response_length", length). - Str("response_mime", mime). Dur("duration", duration) + if resp != nil { + if cli.ResponseHook != nil { + cli.ResponseHook(req, resp, duration) + } + mime := resp.Header.Get("Content-Type") + length := resp.ContentLength + if length == -1 && contentLength > 0 { + length = int64(contentLength) + } + evt = evt.Int("status_code", resp.StatusCode). + Int64("response_length", length). + Str("response_mime", mime) + if serverRequestID := resp.Header.Get("X-Beeper-Request-ID"); serverRequestID != "" { + evt.Str("beeper_request_id", serverRequestID) + } + } if body := req.Context().Value(LogBodyContextKey); body != nil { - evt.Interface("body", body) + evt.Interface("req_body", body) } if handlerErr != nil { evt.AnErr("body_parse_err", handlerErr) } - if serverRequestID := resp.Header.Get("X-Beeper-Request-ID"); serverRequestID != "" { - evt.Str("beeper_request_id", serverRequestID) - } evt.Msg("Request completed") }