From de6c646ee80e1f7aa60b4e50f2051dfe66fc8b94 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Mon, 13 Feb 2017 18:07:35 -0800 Subject: [PATCH] move logging to method in ErrorHandler --- sanic/handlers.py | 11 +++++++++-- sanic/server.py | 6 +++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/sanic/handlers.py b/sanic/handlers.py index 35807213..58dd2f60 100644 --- a/sanic/handlers.py +++ b/sanic/handlers.py @@ -46,7 +46,7 @@ class ErrorHandler: try: response = handler(request=request, exception=exception) except Exception: - log.error(format_exc()) + self.log(format_exc()) if self.debug: response_message = ( 'Exception raised in exception handler "{}" ' @@ -58,8 +58,15 @@ class ErrorHandler: return text('An error occurred while handling an error', 500) return response + def log(self, message, level='error'): + """ + Override this method in an ErrorHandler subclass to prevent + logging exceptions. + """ + getattr(log, level)(message) + def default(self, request, exception): - log.error(format_exc()) + self.log(format_exc()) if issubclass(type(exception), SanicException): return text( 'Error: {}'.format(exception), diff --git a/sanic/server.py b/sanic/server.py index 8b9ed532..8a7969e2 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -346,7 +346,11 @@ def serve(host, port, request_handler, error_handler, before_start=None, # Register signals for graceful termination if register_sys_signals: for _signal in (SIGINT, SIGTERM): - loop.add_signal_handler(_signal, loop.stop) + try: + loop.add_signal_handler(_signal, loop.stop) + except NotImplementedError: + log.warn(('Sanic tried to use loop.add_signal_handler') + ('but it is not implemented on this platform.')) pid = os.getpid() try: