]> granicus.if.org Git - python/commitdiff
Closes #12419: Added ident to SysLogHandler.
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Mon, 27 Jun 2011 14:40:06 +0000 (15:40 +0100)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>
Mon, 27 Jun 2011 14:40:06 +0000 (15:40 +0100)
Doc/library/logging.handlers.rst
Lib/logging/handlers.py
Lib/test/test_logging.py

index e8353ea5afe9eed41d0363588961ecbcf6d6ef34..82e396903e21b61fcc68a4770648a705eca746c8 100644 (file)
@@ -452,6 +452,15 @@ supports sending logging messages to a remote or local Unix syslog.
          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
index 5779a7dbf3a2e353164215416002afa253b7e070..ef17081908da55bb5e4bb77540443bc40273915c 100644 (file)
@@ -769,6 +769,7 @@ class SysLogHandler(logging.Handler):
         """
         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 @@ class SysLogHandler(logging.Handler):
         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'
         """
index b0b8e19068917116332a5c0f9699f001a2f27dea..5d84114f0cdbd8e776a87a502402f2a38be7ca10 100644 (file)
@@ -1482,6 +1482,11 @@ class SysLogHandlerTest(BaseTest):
         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.')