8000 bpo-34334: Don't log traceback twice in QueueHandler (GH-9537) (GH-9581) · python/cpython@1a21893 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a21893

Browse files
miss-islingtoncsabella
authored andcommitted
bpo-34334: Don't log traceback twice in QueueHandler (GH-9537) (GH-9581)
(cherry picked from commit d345bb4) Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
1 parent 4322b8d commit 1a21893

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

Doc/library/logging.handlers.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -973,9 +973,9 @@ possible, while any potentially slow operations (such as sending an email via
973973
Prepares a record for queuing. The object returned by this
974974
method is enqueued.
975975

976-
The base implementation formats the record to merge the message
977-
and arguments, and removes unpickleable items from the record
978-
in-place.
976+
The base implementation formats the record to merge the message,
977+
arguments, and exception information, if present. It also
978+
removes unpickleable items from the record in-place.
979979

980980
You might want to override this method if you want to convert
981981
the record to a dict or JSON string, or send a modified copy

Lib/logging/handlers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,13 +1374,14 @@ def prepare(self, record):
13741374
# (if there's exception data), and also returns the formatted
13751375
# message. We can then use this to replace the original
13761376
# msg + args, as these might be unpickleable. We also zap the
1377-
# exc_info attribute, as it's no longer needed and, if not None,
1378-
# will typically not be pickleable.
1377+
# exc_info and exc_text attributes, as they are no longer
1378+
# needed and, if not None, will typically not be pickleable.
13791379
msg = self.format(record)
13801380
record.message = msg
13811381
record.msg = msg
13821382
record.args = None
13831383
record.exc_info = None
1384+
record.exc_text = None
13841385
return record
13851386

13861387
def emit(self, record):

Lib/test/test_logging.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,6 +3278,21 @@ def test_queue_listener(self):
32783278
self.assertFalse(handler.matches(levelno=logging.WARNING, message='4'))
32793279
self.assertFalse(handler.matches(levelno=logging.ERROR, message='5'))
32803280
self.assertTrue(handler.matches(levelno=logging.CRITICAL, message='6'))
3281+
handler.close()
3282+
3283+
@unittest.skipUnless(hasattr(logging.handlers, 'QueueListener'),
3284+
'logging.handlers.QueueListener required for this test')
3285+
def test_queue_listener_with_StreamHandler(self):
3286+
# Test that traceback only appends once (bpo-34334).
3287+
listener = logging.handlers.QueueListener(self.queue, self.root_hdlr)
3288+
listener.start()
3289+
try:
3290+
1 / 0
3291+
except ZeroDivisionError as e:
3292+
exc = e
3293+
self.que_logger.exception(self.next_message(), exc_info=exc)
3294+
listener.stop()
3295+
self.assertEqual(self.stream.getvalue().strip().count('Traceback'), 1)
32813296

32823297
if hasattr(logging.handlers, 'QueueListener'):
32833298
import multiprocessing
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In :class:`QueueHandler`, clear `exc_text` from :class:`LogRecord` to
2+
prevent traceback from being written twice.

0 commit comments

Comments
 (0)
0