Skip to content

Show response body when debug logging enabled #1186

@raxod502

Description

@raxod502

Suppose I'm getting an HTTP error from the server DAV endpoints when running vdirsyncer. I then add -vdebug as suggested by the error message from vdirsyncer, expecting to see information would tell me the reason for the failure.

Unfortunately, while vdirsyncer logs the request headers and body, as well as the response status code and headers, it does not log the actual response body from the server, which is where the actual error would most likely be found. Instead, it logs this:

debug: <StreamReader 339 bytes eof>

The log in question is here:

logger.debug(response.content)

I'm not sure how a user is expected to find the error message, except by repeating the HTTP request themselves and seeing what the server returns.

I thought it might be easy to contribute an improvement for this (log the response body on -vdebug), but it turns out to be a bit complicated. Firstly, if you just read the body:

logger.debug(await response.content.read())

Then it consumes the body and it can no longer be read by vdirsyncer subsequently. It would be necessary to make a copy of the response object, but I could not find any API method for doing this exposed by aiohttp or requests.

Next, I noticed the comment:

# TODO: rewrite using
# https://docs.aiohttp.org/en/stable/client_advanced.html#client-tracing

I implemented it, following https://docs.aiohttp.org/en/stable/client_advanced.html#aiohttp-client-tracing and https://docs.aiohttp.org/en/stable/tracing_reference.html#aiohttp.TraceConfig.on_response_chunk_received, and added to vdirsyncer.http:

async def trace_response_chunk_received(session, trace_config_ctx, params):
    logger.debug(params.chunk)

trace_config = aiohttp.TraceConfig()
trace_config.on_response_chunk_received.append(trace_response_chunk_received)

I passed trace_configs=[http.trace_config] to the instantiations of ClientSession in other modules. However, this did not work. I discovered that on_response_chunk_received does not function as documented, as mentioned in aio-libs/aiohttp#5324.

At this point I decided to file this issue with my findings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions