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

Show requests log in terminal #2952

Closed
cyantarek opened this issue Apr 23, 2018 · 17 comments
Closed

Show requests log in terminal #2952

cyantarek opened this issue Apr 23, 2018 · 17 comments
Labels

Comments

@cyantarek
Copy link

Long story short

I want the log display in terminal feature like django

Expected behaviour

When running "python main.py", any hit on URL endpoint should log the info in the terminal.

For example:

======== Running on http://0.0.0.0:8095 ========
(Press CTRL+C to quit)
[21:36:52] 127.0.0.1 [23/Apr/2018:15:36:52 +0000] "GET / HTTP/1.1" 200 183 "-" "PostmanRuntime/6.4.1"

Actual behaviour

No log info display

Your environment

Windows 8.1, 64 Bit, Python 3.6

@asvetlov
Copy link
Member

You need to setup access logger: https://docs.aiohttp.org/en/stable/logging.html#access-logs

All-logging-by-default is too annoying, that's why aiohttp has a separate API for it

@cyantarek
Copy link
Author

Well, after that, will it be shown in terminal for each requests?

@drpoggi
Copy link

drpoggi commented Apr 24, 2018

Well, after that, will it be shown in terminal for each requests?

Yes.

@cyantarek
Copy link
Author

I'm new to aiohttp, can you show me how to configure that?

@asvetlov
Copy link
Member

The question is out of aiohttp scope.
Perhaps you should learn about Python logging: https://docs.python.org/3/library/logging.html#logging.basicConfig

@scherrey
Copy link

Actually your aiohttp logging docs implies there's extra stuff or a best practice to do logging for aiohttp apps and thus creates confusion for the issue. I just ran into the exact same issue, especially since the documentation actually explicitly says logging is turned on by default but nothing comes out. And anyone who has ever written a web server or was working to get familiar with a web framework has needed this ability for initial tests/debugging - so an example of the simplest way to turn this on for web development would actually be quite valuable in the documentation. I thought for a moment that the logging utilities might be implemented as a context or something but that doesn't seem to be the case.

@scherrey
Copy link

For debugging purposes, I made this little bash command that will run my app (listing_server.listing_app) with debug logging turned on. It should make it obvious how to use basic logging in an aiohttp server context.

python -c "import logging; from aiohttp import web; from listing_server import *; logging.basicConfig(level=logging.DEBUG); web.run_app(listing_app);"

@asvetlov
Copy link
Member

Logging setup is a very complicated question: different apps require different settings.
For example, one app wants logging rotation, another stores logs into logstash for ELK stack etc. etc.
Pushing everything into run_app makes a mess very easily.

If you have an idea for documentation improvement -- please make a Pull Request.

@xen
Copy link
Contributor

xen commented Sep 5, 2018

Actually, access log to the stdout is standard for any HTTP servers.

@asvetlov
Copy link
Member

asvetlov commented Sep 5, 2018

Really?
IIRC flask requires explicit logging system setup like aiohttp: http://flask.pocoo.org/docs/1.0/logging/

@webknjaz
Copy link
Member

webknjaz commented Sep 5, 2018

AFAIR flask prints out access logs too

@asvetlov
Copy link
Member

asvetlov commented Sep 6, 2018

Aha, http://flask.pocoo.org/docs/1.0/logging/#default-configuration
Flask adds StreamHandler to application logger if the logger has no handlers at the moment of application startup.
We can do the same in web.run_app. AppRunner should be configured explicitly.

@sjatkins
Copy link

sjatkins commented Apr 4, 2019

No real answer is given. Or none that is intelligible to this reader. Give clear minimal instructions for running your web app with access logging please.

@ixje
Copy link

ixje commented Apr 9, 2019

Here's an example that worked for my situation

import logging

stdio_handler = logging.StreamHandler()
stdio_handler.setLevel(logging.INFO)
_logger = logging.getLogger('aiohttp.access')
_logger.addHandler(stdio_handler)
_logger.setLevel(logging.DEBUG)
app = web.Application(logger=_logger)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, args.host, args.port_rpc)
await site.start()

and the output now includes lines like

127.0.0.1 [09/Apr/2019:10:59:18 +0000] "POST / HTTP/1.1" 200 272 "-" "PostmanRuntime/7.6.1"

@NewUserHa
Copy link
Contributor

hope there's a document enhancement or an aiohttp enhancement for this.
thanks you

@asvetlov
Copy link
Member

Champion is welcome!

@NewUserHa
Copy link
Contributor

I have no idea how to modify aiohttp to behavior like others like flask. maybe use ixje's comment?
if you don't mind, I can PR a document enhancement about 'logging.basicConfig()' that you comment in another issue.

ps. the PR for debug flag to run_app cant pass test. do you have any idea?
thanks

@lock lock bot added the outdated label Jun 24, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Jun 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

9 participants