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

Skip to content

Commit adfe344

Browse files
favllvsajip
authored andcommitted
bpo-31084: QueueHandler now formats messages correctly. (GH-2954)
1 parent 6f446be commit adfe344

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

Lib/logging/handlers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,13 +1372,14 @@ def prepare(self, record):
13721372
of the record while leaving the original intact.
13731373
"""
13741374
# The format operation gets traceback text into record.exc_text
1375-
# (if there's exception data), and also puts the message into
1376-
# record.message. We can then use this to replace the original
1375+
# (if there's exception data), and also returns the formatted
1376+
# message. We can then use this to replace the original
13771377
# msg + args, as these might be unpickleable. We also zap the
13781378
# exc_info attribute, as it's no longer needed and, if not None,
13791379
# will typically not be pickleable.
1380-
self.format(record)
1381-
record.msg = record.message
1380+
msg = self.format(record)
1381+
record.message = msg
1382+
record.msg = msg
13821383
record.args = None
13831384
record.exc_info = None
13841385
return record

Lib/test/test_logging.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,6 +3126,7 @@ def setUp(self):
31263126
BaseTest.setUp(self)
31273127
self.queue = queue.Queue(-1)
31283128
self.que_hdlr = logging.handlers.QueueHandler(self.queue)
3129+
self.name = 'que'
31293130
self.que_logger = logging.getLogger('que')
31303131
self.que_logger.propagate = False
31313132
self.que_logger.setLevel(logging.WARNING)
@@ -3147,6 +3148,19 @@ def test_queue_handler(self):
31473148
self.assertEqual(data.name, self.que_logger.name)
31483149
self.assertEqual((data.msg, data.args), (msg, None))
31493150

3151+
def test_formatting(self):
3152+
msg = self.next_message()
3153+
levelname = logging.getLevelName(logging.WARNING)
3154+
log_format_str = '{name} -> {levelname}: {message}'
3155+
formatted_msg = log_format_str.format(name=self.name,
3156+
levelname=levelname, message=msg)
3157+
formatter = logging.Formatter(self.log_format)
3158+
self.que_hdlr.setFormatter(formatter)
3159+
self.que_logger.warning(msg)
3160+
log_record = self.queue.get_nowait()
3161+
self.assertEqual(formatted_msg, log_record.msg)
3162+
self.assertEqual(formatted_msg, log_record.message)
3163+
31503164
@unittest.skipUnless(hasattr(logging.handlers, 'QueueListener'),
31513165
'logging.handlers.QueueListener required for this test')
31523166
def test_queue_listener(self):

0 commit comments

Comments
 (0)