How to set up logging for aiohttp.client when making request with aiohttp.ClientSession()?
As you can see in aiohttp's code, the aiohttp.client logger is not used to log requests, but can only log a warning if the cookies in the response are invalid https://github.com/aio-libs/aiohttp/search?utf8=%E2%9C%93&q=client_logger&type=
To log every requests you are doing, you will need to create a custom ClientSession
that does what you want. Something like:
class LoggingClientSession(aiohttp.ClientSession):
def request(self, method, url, **kwargs):
logger.debug('Starting request <%s %r>', method, url)
return super().request(method, url, **kwargs)
-
As noted by Jaanus in the comments here, the post
, get
, … helpers now call ClientSession._request
directly, instead of request
. So overriding the later won't intercept calls made with the short-hand helpers.
So you can either:
override
_request
instead ofrequest
in your helperor make sure your code never uses the
get
/… helpers and always callsrequest
directly.or also define all the helper methods in your
LoggingClientSession
-
And as noted by Romuald, _request
is now a coroutine, so overriding it with a regular function will not log at exactly the right time. Here is an updated example:
class LoggingClientSession(aiohttp.ClientSession):
async def _request(self, method, url, **kwargs):
logger.debug('Starting request <%s %r>', method, url)
return await super()._request(method, url, **kwargs)