[Python-checkins] r78055 - in python/trunk: Lib/logging/__init__.py Lib/test/test_logging.py Misc/NEWS

vinay.sajip python-checkins at python.org
Sun Feb 7 02:37:08 CET 2010


Author: vinay.sajip
Date: Sun Feb  7 02:37:08 2010
New Revision: 78055

Log:
Issue #7868: logging: added loggerClass attribute to Manager.

Modified:
   python/trunk/Lib/logging/__init__.py
   python/trunk/Lib/test/test_logging.py
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/logging/__init__.py
==============================================================================
--- python/trunk/Lib/logging/__init__.py	(original)
+++ python/trunk/Lib/logging/__init__.py	Sun Feb  7 02:37:08 2010
@@ -1,4 +1,4 @@
-# Copyright 2001-2009 by Vinay Sajip. All Rights Reserved.
+# Copyright 2001-2010 by Vinay Sajip. All Rights Reserved.
 #
 # Permission to use, copy, modify, and distribute this software and its
 # documentation for any purpose and without fee is hereby granted,
@@ -46,8 +46,8 @@
 
 __author__  = "Vinay Sajip <vinay_sajip at red-dove.com>"
 __status__  = "production"
-__version__ = "0.5.1.1"
-__date__    = "25 November 2009"
+__version__ = "0.5.1.2"
+__date__    = "07 February 2010"
 
 #---------------------------------------------------------------------------
 #   Miscellaneous module data
@@ -962,6 +962,7 @@
         self.disable = 0
         self.emittedNoHandlerWarning = 0
         self.loggerDict = {}
+        self.loggerClass = None
 
     def getLogger(self, name):
         """
@@ -981,13 +982,13 @@
                 rv = self.loggerDict[name]
                 if isinstance(rv, PlaceHolder):
                     ph = rv
-                    rv = _loggerClass(name)
+                    rv = (self.loggerClass or _loggerClass)(name)
                     rv.manager = self
                     self.loggerDict[name] = rv
                     self._fixupChildren(ph, rv)
                     self._fixupParents(rv)
             else:
-                rv = _loggerClass(name)
+                rv = (self.loggerClass or _loggerClass)(name)
                 rv.manager = self
                 self.loggerDict[name] = rv
                 self._fixupParents(rv)
@@ -995,6 +996,16 @@
             _releaseLock()
         return rv
 
+    def setLoggerClass(self, klass):
+        """
+        Set the class to be used when instantiating a logger with this Manager.
+        """
+        if klass != Logger:
+            if not issubclass(klass, Logger):
+                raise TypeError("logger not derived from logging.Logger: "
+                                + klass.__name__)
+        self.loggerClass = klass
+
     def _fixupParents(self, alogger):
         """
         Ensure that there are either loggers or placeholders all the way

Modified: python/trunk/Lib/test/test_logging.py
==============================================================================
--- python/trunk/Lib/test/test_logging.py	(original)
+++ python/trunk/Lib/test/test_logging.py	Sun Feb  7 02:37:08 2010
@@ -1593,7 +1593,6 @@
             logging.config.stopListening()
             t.join(2.0)
 
-    #@unittest.skip("See issue #7857")
     def test_listen_config_10_ok(self):
         with captured_stdout() as output:
             self.setup_via_listener(json.dumps(self.config10))
@@ -1613,7 +1612,6 @@
                 ('ERROR', '4'),
             ], stream=output)
 
-    #@unittest.skip("See issue #7857")
     def test_listen_config_1_ok(self):
         with captured_stdout() as output:
             self.setup_via_listener(textwrap.dedent(ConfigFileTest.config1))
@@ -1629,15 +1627,34 @@
             self.assert_log_lines([])
 
 
+class ManagerTest(BaseTest):
+    def test_manager_loggerclass(self):
+        logged = []
+
+        class MyLogger(logging.Logger):
+            def _log(self, level, msg, args, exc_info=None, extra=None):
+                logged.append(msg)
+
+        man = logging.Manager(None)
+        self.assertRaises(TypeError, man.setLoggerClass, int)
+        man.setLoggerClass(MyLogger)
+        logger = man.getLogger('test')
+        print >> open('/tmp/tmp.txt', 'w'), type(logger)
+        logger.warning('should appear in logged')
+        logging.warning('should not appear in logged')
+
+        self.assertEqual(logged, ['should appear in logged'])
+
+
 # Set the locale to the platform-dependent default.  I have no idea
 # why the test does this, but in any case we save the current locale
 # first and restore it at the end.
 @run_with_locale('LC_ALL', '')
 def test_main():
     run_unittest(BuiltinLevelsTest, BasicFilterTest,
-                    CustomLevelsAndFiltersTest, MemoryHandlerTest,
-                    ConfigFileTest, SocketHandlerTest, MemoryTest,
-                    EncodingTest, WarningsTest, ConfigDictTest)
+                 CustomLevelsAndFiltersTest, MemoryHandlerTest,
+                 ConfigFileTest, SocketHandlerTest, MemoryTest,
+                 EncodingTest, WarningsTest, ConfigDictTest, ManagerTest)
 
 if __name__ == "__main__":
     test_main()

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sun Feb  7 02:37:08 2010
@@ -75,6 +75,8 @@
 Library
 -------
 
+- Issue #7868: logging: added loggerClass attribute to Manager.
+
 - Issue #7851: logging: clarification on logging configuration files.
 
 - Issue #4772: Raise a ValueError when an unknown Bluetooth protocol is


More information about the Python-checkins mailing list