skip built-in exceptions when adding error_summary

We have a lot of cases where we catch a built-in exception then
log. Those exceptions don't add useful information, so do not include
them automatically in the log output.

Change-Id: Ifecb831c0adf4086a9d39feb3eb7e3865db780e6
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2017-05-02 10:57:17 -04:00
parent 426aae569f
commit 0caffbd0db
2 changed files with 26 additions and 0 deletions

View File

@ -363,6 +363,17 @@ class ContextFormatter(logging.Formatter):
# that uses the value simpler.
if not exc_info[0]:
exc_info = None
elif exc_info[0] in (TypeError, ValueError,
KeyError, AttributeError):
# NOTE(dhellmann): Do not include information about
# common built-in exceptions used to detect cases of
# bad or missing data. We don't use isinstance() here
# to limit this filter to only the built-in
# classes. This check is only performed for cases
# where the exception info is being detected
# automatically so if a caller gives us an exception
# we will definitely log it.
exc_info = None
# If we have an exception, format it to be included in the
# output.

View File

@ -657,6 +657,21 @@ class ContextFormatterTestCase(LogTestBase):
expected = 'RuntimeError: test_exception_logging\n'
self.assertTrue(self.stream.getvalue().endswith(expected))
def test_skip_logging_builtin_exceptions(self):
# NOTE(dhellmann): If there is an exception and %(error_summary)s
# does not appear in the format string, ensure that it is
# appended to the end of the log lines.
ctxt = _fake_context()
ctxt.request_id = six.text_type('99')
message = self.trans_fixture.lazy('test ' + six.unichr(128))
for ignore in [ValueError, TypeError, KeyError, AttributeError]:
try:
raise ignore('test_exception_logging')
except ignore as e:
self.log.info(message, context=ctxt)
expected = '{}: {}'.format(e.__class__.__name__, e)
self.assertNotIn(expected, self.stream.getvalue())
def test_exception_logging_format_string(self):
# NOTE(dhellmann): If the format string includes
# %(error_summary)s then ensure the exception message ends up in