Skip to content

Commit

Permalink
Add default logging handler to web.run_app (#3243)
Browse files Browse the repository at this point in the history
  • Loading branch information
0az committed Oct 6, 2018
1 parent 0ebdfaa commit a746006
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES/3324.feature
Original file line number Diff line number Diff line change
@@ -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`.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Andrej Antonov
Andrew Leech
Andrew Lytvyn
Andrew Svetlov
Andrew Zhou
Andrii Soldatenko
Antoine Pietri
Anton Kasyanov
Expand Down
2 changes: 2 additions & 0 deletions aiohttp/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
9 changes: 8 additions & 1 deletion aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions docs/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit a746006

Please sign in to comment.