Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3287e45

Browse files
vsajipemontnemery
andauthored
[3.10] bpo-46755: Don't log stack info twice in QueueHandler (GH-31355) (GH-94565)
Co-authored-by: Erik Montnemery <[email protected]>
1 parent 5bd56a0 commit 3287e45

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

Doc/library/logging.handlers.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,8 +1034,12 @@ 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
1038-
removes unpickleable items from the record in-place.
1037+
arguments, exception and stack information, if present. It also removes
1038+
unpickleable items from the record in-place. Specifically, it overwrites
1039+
the record's :attr:`msg` and :attr:`message` attributes with the merged
1040+
message (obtained by calling the handler's :meth:`format` method), and
1041+
sets the :attr:`args`, :attr:`exc_info` and :attr:`exc_text` attributes
1042+
to ``None``.
10391043

10401044
You might want to override this method if you want to convert
10411045
the record to a dict or JSON string, or send a modified copy
@@ -1047,7 +1051,13 @@ possible, while any potentially slow operations (such as sending an email via
10471051
want to override this if you want to use blocking behaviour, or a
10481052
timeout, or a customized queue implementation.
10491053

1054+
.. attribute:: listener
10501055

1056+
When created via configuration using :func:`~logging.config.dictConfig`, this
1057+
attribute will contain a :class:`QueueListener` instance for use with this
1058+
handler. Otherwise, it will be ``None``.
1059+
1060+
.. versionadded:: 3.12
10511061

10521062
.. _queue-listener:
10531063

Lib/logging/handlers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ def prepare(self, record):
14391439
# (if there's exception data), and also returns the formatted
14401440
# message. We can then use this to replace the original
14411441
# msg + args, as these might be unpickleable. We also zap the
1442-
# exc_info and exc_text attributes, as they are no longer
1442+
# exc_info, exc_text and stack_info attributes, as they are no longer
14431443
# needed and, if not None, will typically not be pickleable.
14441444
msg = self.format(record)
14451445
# bpo-35726: make copy of record to avoid affecting other handlers in the chain.
@@ -1449,6 +1449,7 @@ def prepare(self, record):
14491449
record.args = None
14501450
record.exc_info = None
14511451
record.exc_text = None
1452+
record.stack_info = None
14521453
return record
14531454

14541455
def emit(self, record):

Lib/test/test_logging.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3598,16 +3598,18 @@ def test_queue_listener(self):
35983598
@unittest.skipUnless(hasattr(logging.handlers, 'QueueListener'),
35993599
'logging.handlers.QueueListener required for this test')
36003600
def test_queue_listener_with_StreamHandler(self):
3601-
# Test that traceback only appends once (bpo-34334).
3601+
# Test that traceback and stack-info only appends once (bpo-34334, bpo-46755).
36023602
listener = logging.handlers.QueueListener(self.queue, self.root_hdlr)
36033603
listener.start()
36043604
try:
36053605
1 / 0
36063606
except ZeroDivisionError as e:
36073607
exc = e
36083608
self.que_logger.exception(self.next_message(), exc_info=exc)
3609+
self.que_logger.error(self.next_message(), stack_info=True)
36093610
listener.stop()
36103611
self.assertEqual(self.stream.getvalue().strip().count('Traceback'), 1)
3612+
self.assertEqual(self.stream.getvalue().strip().count('Stack'), 1)
36113613

36123614
@unittest.skipUnless(hasattr(logging.handlers, 'QueueListener'),
36133615
'logging.handlers.QueueListener required for this test')
Lines changed: 2 additions & 0 deletions
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)