Example #1
0
    def test_restores_logging_on_error(self):
        class MyException(Exception):
            pass

        stream = io.StringIO()
        try:
            with helpers.debug(stream=stream):
                raise MyException
        except MyException:
            logger = logging.getLogger('factory')
            self.assertEqual(logger.level, logging.NOTSET)
            self.assertEqual(logger.handlers, [])
Example #2
0
    def test_default_logger(self):
        stream1 = io.StringIO()
        stream2 = io.StringIO()

        l = logging.getLogger('factory.test')
        h = logging.StreamHandler(stream1)
        h.setLevel(logging.INFO)
        l.addHandler(h)

        # Non-debug: no text gets out
        l.debug("Test")
        self.assertEqual('', stream1.getvalue())

        with helpers.debug(stream=stream2):
            # Debug: text goes to new stream only
            l.debug("Test2")

        self.assertEqual('', stream1.getvalue())
        self.assertEqual("Test2\n", stream2.getvalue())
Example #3
0
    def test_alternate_logger(self):
        stream1 = io.StringIO()
        stream2 = io.StringIO()

        l1 = logging.getLogger('factory.test')
        l2 = logging.getLogger('factory.foo')
        h = logging.StreamHandler(stream1)
        h.setLevel(logging.DEBUG)
        l2.addHandler(h)

        # Non-debug: no text gets out
        l1.debug("Test")
        self.assertEqual('', stream1.getvalue())
        l2.debug("Test")
        self.assertEqual('', stream1.getvalue())

        with helpers.debug('factory.test', stream=stream2):
            # Debug: text goes to new stream only
            l1.debug("Test2")
            l2.debug("Test3")

        self.assertEqual("", stream1.getvalue())
        self.assertEqual("Test2\n", stream2.getvalue())