lhk
lhk

Reputation: 30146

logging http requests from aiohttp

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.

Upvotes: 4

Views: 8090

Answers (2)

Samantha Atkins
Samantha Atkins

Reputation: 678

All that was necessary for me was to import logging and add

 logging.basicConfig(level=logging.DEBUG)

in my make_app().

The default access.log format is quite wordy so I calmed it down a bit with the access_log_format argument to web.run_app

web.run_app(app, access_log_format=" :: %r %s %T %t")

Upvotes: 5

Andrew Svetlov
Andrew Svetlov

Reputation: 17376

Client request-response lifecycle is pretty complex, that's why aiohttp doesn't log it.

Please use client tracing to register callbacks for all steps. After that, you can log all events by any convenient method (print, logging, whatever).

Upvotes: 4

Related Questions