Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't understand why access log is not working out of the box #2469

Closed
ricmalta opened this issue Nov 6, 2017 · 7 comments
Closed

Can't understand why access log is not working out of the box #2469

ricmalta opened this issue Nov 6, 2017 · 7 comments

Comments

@ricmalta
Copy link

ricmalta commented Nov 6, 2017

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

@asvetlov
Copy link
Member

asvetlov commented Nov 6, 2017

Add

import logging
logging.basicConfig()

into main() before run_app().

@ricmalta
Copy link
Author

ricmalta commented Nov 6, 2017

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()

@ricmalta
Copy link
Author

ricmalta commented Nov 6, 2017

@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

@asvetlov
Copy link
Member

asvetlov commented Nov 6, 2017

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

@asvetlov
Copy link
Member

No answer -- closing

@NewUserHa
Copy link
Contributor

NewUserHa commented Jun 17, 2019

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..

@nathan-chappell
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants