From 0e33d46ead63922f518c881974df94b82e843f7a Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sat, 29 Sep 2018 22:20:20 +0200 Subject: [PATCH 1/5] Try not to stringify exception in logging messages This just fixes the worst offenders that trip up error reporting tools like Sentry.io --- sanic/__main__.py | 2 +- sanic/handlers.py | 32 ++++++++++++++++++-------------- sanic/server.py | 2 +- sanic/testing.py | 4 +--- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/sanic/__main__.py b/sanic/__main__.py index 594256f8..ebea4ce9 100644 --- a/sanic/__main__.py +++ b/sanic/__main__.py @@ -41,4 +41,4 @@ if __name__ == "__main__": " Example Module: project.sanic_server.app" .format(e.name)) except ValueError as e: - logger.error("{}".format(e)) + logger.exception("Failed to run app") diff --git a/sanic/handlers.py b/sanic/handlers.py index 81dd38d7..dbc7b42a 100644 --- a/sanic/handlers.py +++ b/sanic/handlers.py @@ -1,5 +1,5 @@ import sys -from traceback import format_exc, extract_tb +from traceback import extract_tb from sanic.exceptions import ( ContentRangeError, @@ -83,16 +83,16 @@ class ErrorHandler: if response is None: response = self.default(request, exception) except Exception: - self.log(format_exc()) - if self.debug: - url = getattr(request, 'url', 'unknown') - response_message = ('Exception raised in exception handler ' - '"%s" for uri: "%s"\n%s') - logger.error(response_message, - handler.__name__, url, format_exc()) + try: + url = repr(request.url) + except AttributeError: + url = "unknown" + response_message = ('Exception raised in exception handler ' + '"%s" for uri: %s') + logger.exception(response_message, handler.__name__, url) - return text(response_message % ( - handler.__name__, url, format_exc()), 500) + if self.debug: + return text(response_message % (handler.__name__, url), 500) else: return text('An error occurred while handling an error', 500) return response @@ -105,7 +105,14 @@ class ErrorHandler: getattr(logger, level)(message) def default(self, request, exception): - self.log(format_exc()) + try: + url = repr(request.url) + except AttributeError: + url = "unknown" + + response_message = ('Exception occurred while handling uri: %s') + logger.exception(response_message, url) + if issubclass(type(exception), SanicException): return text( 'Error: {}'.format(exception), @@ -115,9 +122,6 @@ class ErrorHandler: elif self.debug: html_output = self._render_traceback_html(exception, request) - response_message = ('Exception occurred while handling uri: ' - '"%s"\n%s') - logger.error(response_message, request.url, format_exc()) return html(html_output, status=500) else: return html(INTERNAL_SERVER_ERROR_HTML, status=500) diff --git a/sanic/server.py b/sanic/server.py index d5a8f211..2e1248d4 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -441,7 +441,7 @@ class HttpProtocol(asyncio.Protocol): logger.error("Transport closed @ %s and exception " "experienced during error handling", self.transport.get_extra_info('peername')) - logger.debug('Exception:\n%s', traceback.format_exc()) + logger.debug('Exception:', exc_info=True) else: exception = ServerError(message) self.write_error(exception) diff --git a/sanic/testing.py b/sanic/testing.py index 3a1d15c5..e9bf2b5d 100644 --- a/sanic/testing.py +++ b/sanic/testing.py @@ -1,4 +1,3 @@ -import traceback from json import JSONDecodeError from sanic.log import logger from sanic.exceptions import MethodNotSupported @@ -73,8 +72,7 @@ class SanicTestClient: **request_kwargs) results[-1] = response except Exception as e: - logger.error( - 'Exception:\n{}'.format(traceback.format_exc())) + logger.exception('Exception') exceptions.append(e) self.app.stop() From 7d0c0fdf7cbc502292e8e3657e94e6f05619a926 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sat, 29 Sep 2018 22:40:05 +0200 Subject: [PATCH 2/5] fix: Namespacing of sanic logger --- sanic/log.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/log.py b/sanic/log.py index 9c6d868d..67381ce6 100644 --- a/sanic/log.py +++ b/sanic/log.py @@ -58,6 +58,6 @@ LOGGING_CONFIG_DEFAULTS = dict( ) -logger = logging.getLogger('root') +logger = logging.getLogger('sanic.root') error_logger = logging.getLogger('sanic.error') access_logger = logging.getLogger('sanic.access') From 7c9c783e9d8618a0256c9c95f071374a39b0ee34 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 5 Oct 2018 16:30:33 +0200 Subject: [PATCH 3/5] deprecate Handler.log --- sanic/handlers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sanic/handlers.py b/sanic/handlers.py index dbc7b42a..b8c7ded0 100644 --- a/sanic/handlers.py +++ b/sanic/handlers.py @@ -79,10 +79,11 @@ class ErrorHandler: response = None try: if handler: - response = handler(request, exception) + response = handler(req, exception) if response is None: response = self.default(request, exception) except Exception: + self.log(format_exc()) try: url = repr(request.url) except AttributeError: @@ -99,12 +100,11 @@ class ErrorHandler: def log(self, message, level='error'): """ - Override this method in an ErrorHandler subclass to prevent - logging exceptions. + Deprecated, do not use. """ - getattr(logger, level)(message) def default(self, request, exception): + self.log(format_exc()) try: url = repr(request.url) except AttributeError: From 3673feb25622498829b23131a744af939ad162c9 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 5 Oct 2018 16:33:46 +0200 Subject: [PATCH 4/5] fix: typo --- sanic/handlers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/handlers.py b/sanic/handlers.py index b8c7ded0..ac4c28f1 100644 --- a/sanic/handlers.py +++ b/sanic/handlers.py @@ -79,7 +79,7 @@ class ErrorHandler: response = None try: if handler: - response = handler(req, exception) + response = handler(request, exception) if response is None: response = self.default(request, exception) except Exception: From eb93f884f37c16e1c8f8e6f86b9545cfa6bb1a3c Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 5 Oct 2018 16:47:12 +0200 Subject: [PATCH 5/5] fix: Missing import --- sanic/handlers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/handlers.py b/sanic/handlers.py index ac4c28f1..30923e27 100644 --- a/sanic/handlers.py +++ b/sanic/handlers.py @@ -1,5 +1,5 @@ import sys -from traceback import extract_tb +from traceback import extract_tb, format_exc from sanic.exceptions import ( ContentRangeError,