Skip to content

Commit

Permalink
_log -> log
Browse files Browse the repository at this point in the history
  • Loading branch information
hellysmile committed Oct 13, 2017
1 parent a9246ad commit f0ac3e3
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 40 deletions.
13 changes: 0 additions & 13 deletions aiohttp/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,5 @@ def __init__(self, logger, log_format):
self.log_format = log_format

@abstractmethod
def _log(self, request, response, time):
"""
:param request: Request object.
:param response: Response object.
:param float time: Time taken to serve the request.
"""

def log(self, request, response, time):
"""Emit log to logger"""

try:
self._log(request, response, time)
except Exception:
self.logger.exception("Error in logging")
25 changes: 14 additions & 11 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,20 +513,23 @@ def _format_line(self, request, response, time):
return ((key, method(request, response, time))
for key, method in self._methods)

def _log(self, request, response, time):
fmt_info = self._format_line(request, response, time)
def log(self, request, response, time):
try:
fmt_info = self._format_line(request, response, time)

values = list()
extra = dict()
for key, value in fmt_info:
values.append(value)
values = list()
extra = dict()
for key, value in fmt_info:
values.append(value)

if key.__class__ is str:
extra[key] = value
else:
extra[key[0]] = {key[1]: value}
if key.__class__ is str:
extra[key] = value
else:
extra[key[0]] = {key[1]: value}

self.logger.info(self._log_format % tuple(values), extra=extra)
self.logger.info(self._log_format % tuple(values), extra=extra)
except Exception:
self.logger.exception("Error in logging")


class reify:
Expand Down
16 changes: 9 additions & 7 deletions aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
web_urldispatcher, web_ws)
from .abc import AbstractAccessLogger, AbstractMatchInfo, AbstractRouter
from .frozenlist import FrozenList
from .helpers import AccessLogger
from .http import HttpVersion # noqa
from .log import access_logger, web_logger
from .signals import FuncSignal, PostSignal, PreSignal, Signal
Expand Down Expand Up @@ -230,14 +231,14 @@ def router(self):
def middlewares(self):
return self._middlewares

def make_handler(self, *, loop=None, **kwargs):
if 'access_log_class' in kwargs:
access_log_class = kwargs['access_log_class']
def make_handler(self, *,
loop=None,
access_log_class=AccessLogger,
**kwargs):

assert issubclass(access_log_class, AbstractAccessLogger), (
'{} must be subclass of '
'aiohttp.abc.AbstractAccessLogger'.format(access_log_class)
)
if not issubclass(access_log_class, AbstractAccessLogger):
raise TypeError('access_log_class must be subclass of '
'aiohttp.abc.AbstractAccessLogger')

self._set_loop(loop)
self.freeze()
Expand All @@ -248,6 +249,7 @@ def make_handler(self, *, loop=None, **kwargs):
kwargs[k] = v

return Server(self._handle, request_factory=self._make_request,
access_log_class=access_log_class,
loop=self.loop, **kwargs)

@asyncio.coroutine
Expand Down
4 changes: 2 additions & 2 deletions docs/abc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ Abstract Abstract Access Logger
An abstract class, base for all :class:`RequestHandler`
``access_logger`` implementations

Method ``_log`` should be overridden.
Method ``log`` should be overridden.

.. method:: _log(request, response, time)
.. method:: log(request, response, time)

:param request: :class:`aiohttp.web.Request` object.

Expand Down
2 changes: 1 addition & 1 deletion docs/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Example of drop-in replacement for :class:`aiohttp.helpers.AccessLogger`::

class AccessLogger(AbstractAccessLogger):

def _log(self, request, response, time):
def log(self, request, response, time):
self.logger.info(f'{request.remote} '
f'"{request.method} {request.path} '
f'done in {time}s: {response.status}')
Expand Down
9 changes: 5 additions & 4 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,17 @@ def test_logger_no_transport():

def test_logger_abc():
class Logger(AbstractAccessLogger):
def _log(self, request, response, time):
def log(self, request, response, time):
1 / 0

mock_logger = mock.Mock()
access_logger = Logger(mock_logger, None)
access_logger.log(None, None, None)
mock_logger.exception.assert_called_with("Error in logging")

with pytest.raises(ZeroDivisionError):
access_logger.log(None, None, None)

class Logger(AbstractAccessLogger):
def _log(self, request, response, time):
def log(self, request, response, time):
self.logger.info(self.log_format.format(
request=request,
response=response,
Expand Down
6 changes: 4 additions & 2 deletions tests/test_web_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def test_app_make_handler_debug_exc(loop, mocker, debug):
app.make_handler(loop=loop)
srv.assert_called_with(app._handle,
request_factory=app._make_request,
access_log_class=mock.ANY,
loop=loop,
debug=debug)

Expand All @@ -76,6 +77,7 @@ def test_app_make_handler_args(loop, mocker):
app.make_handler(loop=loop)
srv.assert_called_with(app._handle,
request_factory=app._make_request,
access_log_class=mock.ANY,
loop=loop, debug=mock.ANY, test=True)


Expand All @@ -85,12 +87,12 @@ class Logger:

app = web.Application()

with pytest.raises(AssertionError):
with pytest.raises(TypeError):
app.make_handler(access_log_class=Logger, loop=loop)

class Logger(AbstractAccessLogger):

def _log(self, request, response, time):
def log(self, request, response, time):
self.logger.info('msg')

srv = mocker.patch('aiohttp.web.Server')
Expand Down

0 comments on commit f0ac3e3

Please sign in to comment.