From e3852ceeca94317b11e0c24841d1678abf8806e1 Mon Sep 17 00:00:00 2001 From: Piotr Bulinski Date: Wed, 4 Oct 2017 12:50:57 +0200 Subject: [PATCH 1/3] Refactor access log for server --- sanic/server.py | 63 +++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 36 deletions(-) diff --git a/sanic/server.py b/sanic/server.py index 12efc180..e66afcf8 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -300,6 +300,28 @@ class HttpProtocol(asyncio.Protocol): # -------------------------------------------- # # Responding # -------------------------------------------- # + def log_response(self, response): + if self.has_log: + extra = { + 'status': getattr(response, 'status', 0), + } + + if isinstance(response, HTTPResponse): + extra['byte'] = len(response.body) + else: + extra['byte'] = -1 + + if self.request: + extra['host'] = '{0}:{1}'.format(self.request.ip[0], + self.request.ip[1]) + extra['request'] = '{0} {1}'.format(self.request.method, + self.request.url) + else: + extra['host'] = 'UNKNOWN' + extra['request'] = 'nil' + + netlog.info('', extra=extra) + def write_response(self, response): """ Writes response content synchronously to the transport. @@ -313,15 +335,7 @@ class HttpProtocol(asyncio.Protocol): response.output( self.request.version, keep_alive, self.keep_alive_timeout)) - if self.has_log: - netlog.info('', extra={ - 'status': response.status, - 'byte': len(response.body), - 'host': '{0}:{1}'.format(self.request.ip[0], - self.request.ip[1]), - 'request': '{0} {1}'.format(self.request.method, - self.request.url) - }) + self.log_response(response) except AttributeError: log.error( ('Invalid response object for url {}, ' @@ -360,15 +374,7 @@ class HttpProtocol(asyncio.Protocol): response.transport = self.transport await response.stream( self.request.version, keep_alive, self.keep_alive_timeout) - if self.has_log: - netlog.info('', extra={ - 'status': response.status, - 'byte': -1, - 'host': '{0}:{1}'.format(self.request.ip[0], - self.request.ip[1]), - 'request': '{0} {1}'.format(self.request.method, - self.request.url) - }) + self.log_response(response) except AttributeError: log.error( ('Invalid response object for url {}, ' @@ -414,24 +420,9 @@ class HttpProtocol(asyncio.Protocol): repr(e)), from_error=True ) finally: - if self.has_log: - extra = dict() - if isinstance(response, HTTPResponse): - extra['status'] = response.status - extra['byte'] = len(response.body) - else: - extra['status'] = 0 - extra['byte'] = -1 - if self.request: - extra['host'] = '%s:%d' % self.request.ip, - extra['request'] = '%s %s' % (self.request.method, - self.url) - else: - extra['host'] = 'UNKNOWN' - extra['request'] = 'nil' - if self.parser and not (self.keep_alive - and extra['status'] == 408): - netlog.info('', extra=extra) + if self.parser and (self.keep_alive + or getattr(response, 'status', 0) == 408): + self.log_response(response) self.transport.close() def bail_out(self, message, from_error=False): From 8ce749e339509e30e4e87bc854da4200cc2409c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Buli=C5=84ski?= Date: Thu, 5 Oct 2017 09:27:18 +0200 Subject: [PATCH 2/3] Update server.py --- sanic/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/server.py b/sanic/server.py index a17e479b..8f67901d 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -320,7 +320,7 @@ class HttpProtocol(asyncio.Protocol): extra['host'] = 'UNKNOWN' extra['request'] = 'nil' - netlog.info('', extra=extra) + access_logger.info('', extra=extra) def write_response(self, response): """ From 4b877e3f6bab0a754fa1907970e1b0842e824995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Buli=C5=84ski?= Date: Thu, 5 Oct 2017 09:28:13 +0200 Subject: [PATCH 3/3] Update server.py --- sanic/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/server.py b/sanic/server.py index 8f67901d..e151c54f 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -301,7 +301,7 @@ class HttpProtocol(asyncio.Protocol): # Responding # -------------------------------------------- # def log_response(self, response): - if self.has_log: + if self.access_log: extra = { 'status': getattr(response, 'status', 0), }