summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Hellmann <doug@doughellmann.com>2017-05-02 10:57:17 -0400
committerDoug Hellmann <doug@doughellmann.com>2017-05-15 16:18:49 -0400
commit0caffbd0db2f83c7c632884abaade0aa96a7321e (patch)
tree6928e4b60d835216d95efd4e3ee015f34fa06938
parent426aae569f93e0b367b6636becf43448596b3c60 (diff)
downloadoslo-log-0caffbd0db2f83c7c632884abaade0aa96a7321e.tar.gz
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>
-rw-r--r--oslo_log/formatters.py11
-rw-r--r--oslo_log/tests/unit/test_log.py15
2 files changed, 26 insertions, 0 deletions
diff --git a/oslo_log/formatters.py b/oslo_log/formatters.py
index e6faebc..fc1bc51 100644
--- a/oslo_log/formatters.py
+++ b/oslo_log/formatters.py
@@ -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.
diff --git a/oslo_log/tests/unit/test_log.py b/oslo_log/tests/unit/test_log.py
index a4405de..acf6f95 100644
--- a/oslo_log/tests/unit/test_log.py
+++ b/oslo_log/tests/unit/test_log.py
@@ -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