Skip to content

gh-137790 Add an option to logging.Formatter to always set record.exc_text even if it's not None #137802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4570,6 +4570,9 @@ def setUp(self):
self.variants = {
'custom': {
'custom': 1234
},
'exception': {
'exc_info': ZeroDivisionError()
}
}

Expand Down Expand Up @@ -4909,6 +4912,34 @@ def test_relativeCreated_has_higher_precision(self):
# After PR gh-102412, precision (places) increases from 3 to 7
self.assertAlmostEqual(relativeCreated, offset_ns / 1e6, places=7)

def test_multiple_formatters_exc_text(self):
# Regression test for current behavior where exc_text is only set if it is None
r = self.get_record()
r.exc_info = (ZeroDivisionError, ZeroDivisionError(), None)

f = logging.Formatter('${%(message)s}')
f.format(r)
self.assertIsNotNone(r.exc_text)
exc_text = r.exc_text

f = logging.Formatter('%(asctime)s')
f.format(r)
self.assertEqual(exc_text, r.exc_text)

def test_multiple_formatters_set_exc_text(self):
# Tests that exc_text is changed when set_exc_text option is set on the Formatter
r = self.get_record()
r.exc_info = (ZeroDivisionError, ZeroDivisionError(), None)

f = logging.Formatter('${%(message)s}')
f.format(r)
self.assertIsNotNone(r.exc_text)
exc_text = r.exc_text

f = logging.Formatter('%(asctime)s', set_exc_text=True)
f.format(r)
self.assertNotEqual(exc_text, r.exc_text)


class TestBufferingFormatter(logging.BufferingFormatter):
def formatHeader(self, records):
Expand Down Expand Up @@ -5010,7 +5041,6 @@ def inner():


class RecordingHandler(logging.NullHandler):

def __init__(self, *args, **kwargs):
super(RecordingHandler, self).__init__(*args, **kwargs)
self.records = []
Expand Down
Loading