I would like to log all HTTP requests sent by an aiohttp ClientSession. The docs provide a list of available loggers. So I tried the following:
import asyncio
import logging
import aiohttp
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
http_logger = logging.getLogger("aiohttp.client")
http_logger.setLevel(logging.DEBUG)
http_logger.propagate = True
async def make_request():
async with aiohttp.ClientSession() as session:
async with session.get('https://httpbin.org/get') as resp:
return await resp.text()
loop = asyncio.get_event_loop()
response_text = loop.run_until_complete(make_request())
print(response_text)
But this only produced the following output:
DEBUG:asyncio:Using selector: EpollSelector
// response text print here
I tried all the loggers from that list in the docs and then searched for questions. This one is similar: specify log request format in aiohttp 2
The answer describes how to set up logging for an aiohttp server. Interestingly, they have to explicitly register the logger:
app = web.Application(loop=loop)
app.router.add_get('/', handle)
app.router.add_get('/{name}', handle)
loop.run_until_complete(
loop.create_server(
app.make_handler(access_log=mylogger, #<--------------- HERE
access_log_format='%r %s %b'), '0.0.0.0', 8080))
Is this necessary for the client, too? The only way where I could reasonably inject my logger would probably be the session. But the API reference for the ClientSession doesn't specify any logger argument.