diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2010-12-10 11:42:57 (GMT) |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2010-12-10 11:42:57 (GMT) |
commit | 5a27d4018692a9cb9f30b5ed81ac7061c8fe3c82 (patch) | |
tree | 13607ce1891690d5d533633bf673b3222a67a1dd /Lib/test | |
parent | cf03ac0c64dba3ec33f9c3af1cbe7b387d1ca534 (diff) | |
download | cpython-5a27d4018692a9cb9f30b5ed81ac7061c8fe3c82.zip cpython-5a27d4018692a9cb9f30b5ed81ac7061c8fe3c82.tar.gz cpython-5a27d4018692a9cb9f30b5ed81ac7061c8fe3c82.tar.bz2 |
logging: added handler of last resort.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_logging.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index cd8cdbd..08fd7c4 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -1927,6 +1927,40 @@ class FormatterTest(unittest.TestCase): f = logging.Formatter('asctime', style='$') self.assertFalse(f.usesTime()) +class LastResortTest(BaseTest): + def test_last_resort(self): + "Test the last resort handler" + root = self.root_logger + root.removeHandler(self.root_hdlr) + old_stderr = sys.stderr + old_lastresort = logging.lastResort + old_raise_exceptions = logging.raiseExceptions + try: + sys.stderr = sio = io.StringIO() + root.warning('This is your final chance!') + self.assertEqual(sio.getvalue(), 'This is your final chance!\n') + #No handlers and no last resort, so 'No handlers' message + logging.lastResort = None + sys.stderr = sio = io.StringIO() + root.warning('This is your final chance!') + self.assertEqual(sio.getvalue(), 'No handlers could be found for logger "root"\n') + # 'No handlers' message only printed once + sys.stderr = sio = io.StringIO() + root.warning('This is your final chance!') + self.assertEqual(sio.getvalue(), '') + root.manager.emittedNoHandlerWarning = False + #If raiseExceptions is False, no message is printed + logging.raiseExceptions = False + sys.stderr = sio = io.StringIO() + root.warning('This is your final chance!') + self.assertEqual(sio.getvalue(), '') + finally: + sys.stderr = old_stderr + root.addHandler(self.root_hdlr) + logging.lastResort = old_lastresort + logging.raiseExceptions = old_raise_exceptions + + class BaseFileTest(BaseTest): "Base class for handler tests that write log files" @@ -2017,6 +2051,7 @@ def test_main(): FormatterTest, LogRecordFactoryTest, ChildLoggerTest, QueueHandlerTest, RotatingFileHandlerTest, + LastResortTest, #TimedRotatingFileHandlerTest ) |