[Python-checkins] cpython: Closes #12419: Added ident to SysLogHandler.

vinay.sajip python-checkins at python.org
Mon Jun 27 16:40:47 CEST 2011


http://hg.python.org/cpython/rev/6baa90fa2b6d
changeset:   71015:6baa90fa2b6d
user:        Vinay Sajip <vinay_sajip at yahoo.co.uk>
date:        Mon Jun 27 15:40:06 2011 +0100
summary:
  Closes #12419: Added ident to SysLogHandler.

files:
  Doc/library/logging.handlers.rst |  9 +++++++++
  Lib/logging/handlers.py          |  3 +++
  Lib/test/test_logging.py         |  5 +++++
  3 files changed, 17 insertions(+), 0 deletions(-)


diff --git a/Doc/library/logging.handlers.rst b/Doc/library/logging.handlers.rst
--- a/Doc/library/logging.handlers.rst
+++ b/Doc/library/logging.handlers.rst
@@ -452,6 +452,15 @@
          behaviour) but can be set to ``False`` on a ``SysLogHandler`` instance
          in order for that instance to *not* append the NUL terminator.
 
+      .. versionchanged:: 3.3
+         (See: :issue:`12419`.) In earlier versions, there was no facility for
+         an "ident" or "tag" prefix to identify the source of the message. This
+         can now be specified using a class-level attribute, defaulting to
+         ``""`` to preserve existing behaviour, but which can be overridden on
+         a ``SysLogHandler`` instance in order for that instance to prepend
+         the ident to every message handled. Note that the provided ident must
+         be text, not bytes, and is prepended to the message exactly as is.
+
    .. method:: encodePriority(facility, priority)
 
       Encodes the facility and priority into an integer. You can pass in strings
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -769,6 +769,7 @@
         """
         return self.priority_map.get(levelName, "warning")
 
+    ident = ''          # prepended to all messages
     append_nul = True   # some old syslog daemons expect a NUL terminator
 
     def emit(self, record):
@@ -779,6 +780,8 @@
         exception information is present, it is NOT sent to the server.
         """
         msg = self.format(record)
+        if self.ident:
+            msg = self.ident + msg
         if self.append_nul:
             msg += '\000'
         """
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -1482,6 +1482,11 @@
         logger.error("sp\xe4m")
         self.handled.wait()
         self.assertEqual(self.log_output, b'<11>\xef\xbb\xbfsp\xc3\xa4m')
+        self.handled.clear()
+        self.sl_hdlr.ident = "h\xe4m-"
+        logger.error("sp\xe4m")
+        self.handled.wait()
+        self.assertEqual(self.log_output, b'<11>\xef\xbb\xbfh\xc3\xa4m-sp\xc3\xa4m')
 
 
 @unittest.skipUnless(threading, 'Threading required for this test.')

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list