Skip to content

Commit c60f125

Browse files
authored
bpo-46755: Don't log stack info twice in QueueHandler (GH-31355)
1 parent 324d019 commit c60f125

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

Doc/library/logging.handlers.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ possible, while any potentially slow operations (such as sending an email via
10341034
method is enqueued.
10351035

10361036
The base implementation formats the record to merge the message,
1037-
arguments, and exception information, if present. It also removes
1037+
arguments, exception and stack information, if present. It also removes
10381038
unpickleable items from the record in-place. Specifically, it overwrites
10391039
the record's :attr:`msg` and :attr:`message` attributes with the merged
10401040
message (obtained by calling the handler's :meth:`format` method), and

Lib/logging/handlers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ def prepare(self, record):
14561456
# (if there's exception data), and also returns the formatted
14571457
# message. We can then use this to replace the original
14581458
# msg + args, as these might be unpickleable. We also zap the
1459-
# exc_info and exc_text attributes, as they are no longer
1459+
# exc_info, exc_text and stack_info attributes, as they are no longer
14601460
# needed and, if not None, will typically not be pickleable.
14611461
msg = self.format(record)
14621462
# bpo-35726: make copy of record to avoid affecting other handlers in the chain.
@@ -1466,6 +1466,7 @@ def prepare(self, record):
14661466
record.args = None
14671467
record.exc_info = None
14681468
record.exc_text = None
1469+
record.stack_info = None
14691470
return record
14701471

14711472
def emit(self, record):

Lib/test/test_logging.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3808,16 +3808,18 @@ def test_queue_listener(self):
38083808
@unittest.skipUnless(hasattr(logging.handlers, 'QueueListener'),
38093809
'logging.handlers.QueueListener required for this test')
38103810
def test_queue_listener_with_StreamHandler(self):
3811-
# Test that traceback only appends once (bpo-34334).
3811+
# Test that traceback and stack-info only appends once (bpo-34334, bpo-46755).
38123812
listener = logging.handlers.QueueListener(self.queue, self.root_hdlr)
38133813
listener.start()
38143814
try:
38153815
1 / 0
38163816
except ZeroDivisionError as e:
38173817
exc = e
38183818
self.que_logger.exception(self.next_message(), exc_info=exc)
3819+
self.que_logger.error(self.next_message(), stack_info=True)
38193820
listener.stop()
38203821
self.assertEqual(self.stream.getvalue().strip().count('Traceback'), 1)
3822+
self.assertEqual(self.stream.getvalue().strip().count('Stack'), 1)
38213823

38223824
@unittest.skipUnless(hasattr(logging.handlers, 'QueueListener'),
38233825
'logging.handlers.QueueListener required for this test')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In :class:`QueueHandler`, clear ``stack_info`` from :class:`LogRecord` to
2+
prevent stack trace from being written twice.

0 commit comments

Comments
 (0)