Merge pull request #1336 from untitaker/logging-refactor

Try not to stringify exception in logging messages
This commit is contained in:
7 2018-10-13 16:54:57 -07:00 committed by GitHub
commit 619bb79a2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 21 deletions

View File

@ -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")

View File

@ -1,5 +1,5 @@
import sys
from traceback import format_exc, extract_tb
from traceback import extract_tb, format_exc
from sanic.exceptions import (
ContentRangeError,
@ -84,28 +84,35 @@ class ErrorHandler:
response = self.default(request, exception)
except Exception:
self.log(format_exc())
if self.debug:
url = getattr(request, 'url', 'unknown')
try:
url = repr(request.url)
except AttributeError:
url = "unknown"
response_message = ('Exception raised in exception handler '
'"%s" for uri: "%s"\n%s')
logger.error(response_message,
handler.__name__, url, format_exc())
'"%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
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:
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)

View File

@ -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')

View File

@ -436,7 +436,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:
self.write_error(ServerError(message))
logger.error(message)

View File

@ -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()