[Python-checkins] bpo-31084: QueueHandler now formats messages correctly. (GH-2954)

Vinay Sajip webhook-mailer at python.org
Tue Aug 1 14:12:30 EDT 2017


https://github.com/python/cpython/commit/adfe3440f65dfd6cf4463db6cd02cdc78e77ce17
commit: adfe3440f65dfd6cf4463db6cd02cdc78e77ce17
branch: master
author: favll <favll at users.noreply.github.com>
committer: Vinay Sajip <vinay_sajip at yahoo.co.uk>
date: 2017-08-01T19:12:26+01:00
summary:

bpo-31084: QueueHandler now formats messages correctly. (GH-2954)

files:
M Lib/logging/handlers.py
M Lib/test/test_logging.py

diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
index 2f934b33071..b5fdfbc6839 100644
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -1372,13 +1372,14 @@ def prepare(self, record):
         of the record while leaving the original intact.
         """
         # The format operation gets traceback text into record.exc_text
-        # (if there's exception data), and also puts the message into
-        # record.message. We can then use this to replace the original
+        # (if there's exception data), and also returns the formatted
+        # message. We can then use this to replace the original
         # msg + args, as these might be unpickleable. We also zap the
         # exc_info attribute, as it's no longer needed and, if not None,
         # will typically not be pickleable.
-        self.format(record)
-        record.msg = record.message
+        msg = self.format(record)
+        record.message = msg
+        record.msg = msg
         record.args = None
         record.exc_info = None
         return record
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 6d0b23441ba..36ea0725153 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -3126,6 +3126,7 @@ def setUp(self):
         BaseTest.setUp(self)
         self.queue = queue.Queue(-1)
         self.que_hdlr = logging.handlers.QueueHandler(self.queue)
+        self.name = 'que'
         self.que_logger = logging.getLogger('que')
         self.que_logger.propagate = False
         self.que_logger.setLevel(logging.WARNING)
@@ -3147,6 +3148,19 @@ def test_queue_handler(self):
         self.assertEqual(data.name, self.que_logger.name)
         self.assertEqual((data.msg, data.args), (msg, None))
 
+    def test_formatting(self):
+        msg = self.next_message()
+        levelname = logging.getLevelName(logging.WARNING)
+        log_format_str = '{name} -> {levelname}: {message}'
+        formatted_msg = log_format_str.format(name=self.name,
+                                              levelname=levelname, message=msg)
+        formatter = logging.Formatter(self.log_format)
+        self.que_hdlr.setFormatter(formatter)
+        self.que_logger.warning(msg)
+        log_record = self.queue.get_nowait()
+        self.assertEqual(formatted_msg, log_record.msg)
+        self.assertEqual(formatted_msg, log_record.message)
+
     @unittest.skipUnless(hasattr(logging.handlers, 'QueueListener'),
                          'logging.handlers.QueueListener required for this test')
     def test_queue_listener(self):



More information about the Python-checkins mailing list