[Python-checkins] r84965 - in python/branches/py3k: Doc/library/logging.rst Lib/logging/handlers.py

vinay.sajip python-checkins at python.org
Wed Sep 22 22:34:53 CEST 2010


Author: vinay.sajip
Date: Wed Sep 22 22:34:53 2010
New Revision: 84965

Log:
logging: Added QueueHandler.prepare and updated documentation.

Modified:
   python/branches/py3k/Doc/library/logging.rst
   python/branches/py3k/Lib/logging/handlers.py

Modified: python/branches/py3k/Doc/library/logging.rst
==============================================================================
--- python/branches/py3k/Doc/library/logging.rst	(original)
+++ python/branches/py3k/Doc/library/logging.rst	Wed Sep 22 22:34:53 2010
@@ -944,6 +944,7 @@
 instantiated directly, but always through the module-level function
 ``logging.getLogger(name)``.
 
+.. class:: Logger
 
 .. attribute:: Logger.propagate
 
@@ -2661,7 +2662,20 @@
 
    .. method:: emit(record)
 
-      Sends the record to the handler's queue.
+      Enqueues the result of preparing the LogRecord.
+
+   .. method:: prepare(record)
+
+      Prepares a record for queuing. The object returned by this
+      method is enqueued.
+
+      The base implementation formats the record to merge the message
+      and arguments, and removes unpickleable items from the record
+      in-place.
+
+      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
+      of the record while leaving the original intact.
 
    .. method:: enqueue(record)
 

Modified: python/branches/py3k/Lib/logging/handlers.py
==============================================================================
--- python/branches/py3k/Lib/logging/handlers.py	(original)
+++ python/branches/py3k/Lib/logging/handlers.py	Wed Sep 22 22:34:53 2010
@@ -1176,24 +1176,39 @@
         """
         self.queue.put_nowait(record)
 
+    def prepare(self, record):
+        """
+        Prepares a record for queuing. The object returned by this
+        method is enqueued.
+
+        The base implementation formats the record to merge the message
+        and arguments, and removes unpickleable items from the record
+        in-place.
+
+        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
+        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
+        # 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
+        record.args = None
+        record.exc_info = None
+        return record
+
     def emit(self, record):
         """
         Emit a record.
 
-        Writes the LogRecord to the queue, preparing it for pickling first.
+        Writes the LogRecord to the queue, preparing it first.
         """
         try:
-            # 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
-            # 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
-            record.args = None
-            record.exc_info = None
-            self.enqueue(record)
+            self.enqueue(self.prepare(record))
         except (KeyboardInterrupt, SystemExit):
             raise
         except:


More information about the Python-checkins mailing list