Aiohttp: Can't understand why access log is not working out of the box

Created on 6 Nov 2017  路  7Comments  路  Source: aio-libs/aiohttp

Long story short

How do I enable the access log?
I just configured the web interface with the minimum configurations possible. Reading the docs, the logging should work out of the box right?

Here is my main app code:

import asyncio
from aiohttp import web

async def get_index(req):
    """ get the analytics results """
    return web.json_response({ 'ok': True })

def main():
    """ the main application start """

    # the the main event loop
    loop = asyncio.get_event_loop()

    # create the main web app
    app = web.Application(loop=loop)

    # add analytics routes
    app.router.add_get('/v1/analytics', get_index)

    # start the application
    web.run_app(app, host='0.0.0.0', port=8701)

if __name__ == '__main__':
    main()

I debugged aiohttp the code, but wont found anything blocking the logs.
I'm new to Python on the web and aiohttp.

Expected behaviour

I want the default access log to out.

Actual behaviour

nothing shown in the terminal

Steps to reproduce

Just run

Your environment

macOS 10.12.6
Python 3.6.1
aiohttp==2.3.2

need pull request

Most helpful comment

Would you make a Pull Request for http://aiohttp.readthedocs.io/en/stable/logging.html to help other newbies?

All 7 comments

Add

import logging
logging.basicConfig()

into main() before run_app().

Hi @asvetlov. Thanks for the fast response.

Unfortunately it doesn't work. Here is the code:

import asyncio
import logging
from aiohttp import web

async def get_index(req):
    """ get the analytics results """
    return web.json_response({ 'ok': True })

def main():
    """ the main application start """

    # the the main event loop
    loop = asyncio.get_event_loop()

    # create the main web app
    app = web.Application(loop=loop)

    # add analytics routes
    app.router.add_get('/v1/analytics', get_index)

    logging.basicConfig()

    # start the application
    web.run_app(app, host='0.0.0.0', port=8701)

if __name__ == '__main__':
    main()

@asvetlov found the solution by:

Adding logging.basicConfig(level=logging.DEBUG) instead of logging.basicConfig()

Thanks for your help. Worked like a shiny light :D

Would you make a Pull Request for http://aiohttp.readthedocs.io/en/stable/logging.html to help other newbies?

No answer -- closing

logging.getLogger('aiohttp.access').setLevel(logging.DEBUG) dont work.
logging.basicConfig(level=logging.DEBUG) will make all logger to debug level.

is there a good solution to aiohttp?

https://aiohttp.readthedocs.io/en/stable/logging.html

Access logs are enabled by default. If the debug flag is set, and the default logger 'aiohttp.access' is used, access logs will be output to stderr if no handlers are attached. Furthermore, if the default logger has no log level set, the log level will be set to logging.DEBUG.

the doc seems fuzzy..

If anyone ends up here, and needs a quick solution, here's an option that will work. The problem is that the logging library isn't defaulted to be very easy to use, so if you (for example) want to make the aiohttp.web logger log to stdout at the level of DEBUG, then you can do the following:

import aiohttp.web
import logging

app = aiohttp.web.Application()
app.logger.setLevel(logging.DEBUG)
# it is necessary to add a "handler" to the logger,
# or else logging is a nop
app.logger.addHandler(logging.StreamHandler())

app.logger.debug('foo')

Final thoughts: don't do logger.setLevel(0) or you're in for a surprise. The loggers defined by aiohttp are:

access_logger = logging.getLogger('aiohttp.access')
client_logger = logging.getLogger('aiohttp.client')
internal_logger = logging.getLogger('aiohttp.internal')
server_logger = logging.getLogger('aiohttp.server')
web_logger = logging.getLogger('aiohttp.web')
ws_logger = logging.getLogger('aiohttp.websocket')

These can be accessed by calling logging.getLogger('aiohttp.access') etc. The "advanced tutorial" section of the logging api is probably the most informative, I recommend going there first: logging-advanced-tutorial.

Was this page helpful?
0 / 5 - 0 ratings