bpo-34334: Don't log traceback twice in QueueHandler (GH-9537)

This commit is contained in:
Cheryl Sabella 2018-09-25 19:00:08 -04:00 committed by Vinay Sajip
parent fad6af2744
commit d345bb4d9b
4 changed files with 23 additions and 5 deletions

View File

@ -973,9 +973,9 @@ possible, while any potentially slow operations (such as sending an email via
Prepares a record for queuing. The object returned by this Prepares a record for queuing. The object returned by this
method is enqueued. method is enqueued.
The base implementation formats the record to merge the message The base implementation formats the record to merge the message,
and arguments, and removes unpickleable items from the record arguments, and exception information, if present. It also
in-place. removes unpickleable items from the record in-place.
You might want to override this method if you want to convert You might want to override this method if you want to convert
the record to a dict or JSON string, or send a modified copy the record to a dict or JSON string, or send a modified copy

View File

@ -1374,13 +1374,14 @@ class QueueHandler(logging.Handler):
# (if there's exception data), and also returns the formatted # (if there's exception data), and also returns the formatted
# message. We can then use this to replace the original # message. We can then use this to replace the original
# msg + args, as these might be unpickleable. We also zap the # msg + args, as these might be unpickleable. We also zap the
# exc_info attribute, as it's no longer needed and, if not None, # exc_info and exc_text attributes, as they are no longer
# will typically not be pickleable. # needed and, if not None, will typically not be pickleable.
msg = self.format(record) msg = self.format(record)
record.message = msg record.message = msg
record.msg = msg record.msg = msg
record.args = None record.args = None
record.exc_info = None record.exc_info = None
record.exc_text = None
return record return record
def emit(self, record): def emit(self, record):

View File

@ -3345,6 +3345,21 @@ class QueueHandlerTest(BaseTest):
self.assertFalse(handler.matches(levelno=logging.WARNING, message='4')) self.assertFalse(handler.matches(levelno=logging.WARNING, message='4'))
self.assertFalse(handler.matches(levelno=logging.ERROR, message='5')) self.assertFalse(handler.matches(levelno=logging.ERROR, message='5'))
self.assertTrue(handler.matches(levelno=logging.CRITICAL, message='6')) self.assertTrue(handler.matches(levelno=logging.CRITICAL, message='6'))
handler.close()
@unittest.skipUnless(hasattr(logging.handlers, 'QueueListener'),
'logging.handlers.QueueListener required for this test')
def test_queue_listener_with_StreamHandler(self):
# Test that traceback only appends once (bpo-34334).
listener = logging.handlers.QueueListener(self.queue, self.root_hdlr)
listener.start()
try:
1 / 0
except ZeroDivisionError as e:
exc = e
self.que_logger.exception(self.next_message(), exc_info=exc)
listener.stop()
self.assertEqual(self.stream.getvalue().strip().count('Traceback'), 1)
if hasattr(logging.handlers, 'QueueListener'): if hasattr(logging.handlers, 'QueueListener'):
import multiprocessing import multiprocessing

View File

@ -0,0 +1,2 @@
In :class:`QueueHandler`, clear `exc_text` from :class:`LogRecord` to
prevent traceback from being written twice.