diff --git a/CHANGES/3324.feature b/CHANGES/3324.feature new file mode 100644 index 00000000000..c139ce39175 --- /dev/null +++ b/CHANGES/3324.feature @@ -0,0 +1,3 @@ +Add default logging handler to web.run_app + +If the `Application.debug` flag is set and the default logger `aiohttp.access` is used, access logs will now be output using a `stderr` `StreamHandler` if no handlers are attached. Furthermore, if the default logger has no log level set, the log level will be set to `DEBUG`. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index d3db40b4a57..148b84e9d31 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -30,6 +30,7 @@ Andrej Antonov Andrew Leech Andrew Lytvyn Andrew Svetlov +Andrew Zhou Andrii Soldatenko Antoine Pietri Anton Kasyanov diff --git a/aiohttp/log.py b/aiohttp/log.py index 48400473129..e41b9f0254f 100644 --- a/aiohttp/log.py +++ b/aiohttp/log.py @@ -7,3 +7,5 @@ server_logger = logging.getLogger('aiohttp.server') web_logger = logging.getLogger('aiohttp.web') ws_logger = logging.getLogger('aiohttp.websocket') + +default_handler = logging.StreamHandler() diff --git a/aiohttp/web.py b/aiohttp/web.py index 9a824819ceb..a9e88fa7e61 100644 --- a/aiohttp/web.py +++ b/aiohttp/web.py @@ -9,7 +9,7 @@ from . import (helpers, web_app, web_exceptions, web_fileresponse, web_middlewares, web_protocol, web_request, web_response, web_routedef, web_runner, web_server, web_urldispatcher, web_ws) -from .log import access_logger +from .log import access_logger, default_handler from .web_app import * # noqa from .web_exceptions import * # noqa from .web_fileresponse import * # noqa @@ -52,6 +52,13 @@ def run_app(app, *, host=None, port=None, path=None, sock=None, if asyncio.iscoroutine(app): app = loop.run_until_complete(app) + # Configure if and only if in debugging mode and using the default logger + if app.debug and access_log is access_logger: + if access_log.level == logging.NOTSET: + access_log.setLevel(logging.DEBUG) + if not access_log.hasHandlers(): + access_log.addHandler(default_handler) + runner = AppRunner(app, handle_signals=handle_signals, access_log_class=access_log_class, access_log_format=access_log_format, diff --git a/docs/logging.rst b/docs/logging.rst index c6e70554243..ea53aedccf7 100644 --- a/docs/logging.rst +++ b/docs/logging.rst @@ -28,8 +28,11 @@ configuring whole loggers in your application. Access logs ----------- -Access log by default is switched on and uses ``'aiohttp.access'`` -logger name. +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 +:obj:`~sys.stderr` if no handlers are attached. +Furthermore, if the default logger has no log level set, the log level will be +set to :obj:`logging.DEBUG`. The log may be controlled by :meth:`aiohttp.web.AppRunner` and :func:`aiohttp.web.run_app`.