示例#1
0
    def test_fingerscrossed(self):
        from logbook.more import FingersCrossedHandler
        handler = FingersCrossedHandler(logbook.default_handler,
                                        logbook.WARNING)

        # if no warning occurs, the infos are not logged
        handler.push_thread()
        try:
            captured = capture_stderr.start()
            try:
                self.log.info('some info')
            finally:
                capture_stderr.end()
            self.assertEqual(captured.getvalue(), '')
            self.assert_(not handler.triggered)
        finally:
            handler.pop_thread()

        # but if it does, all log messages are output
        handler.push_thread()
        try:
            captured = capture_stderr.start()
            try:
                self.log.info('some info')
                self.log.warning('something happened')
                self.log.info('something else happened')
            finally:
                capture_stderr.end()
            logs = captured.getvalue()
            self.assert_('some info' in logs)
            self.assert_('something happened' in logs)
            self.assert_('something else happened' in logs)
            self.assert_(handler.triggered)
        finally:
            handler.pop_thread()
示例#2
0
    def test_fingerscrossed_buffer_size(self):
        from logbook.more import FingersCrossedHandler
        logger = logbook.Logger('Test')
        test_handler = logbook.TestHandler()
        handler = FingersCrossedHandler(test_handler, buffer_size=3)

        handler.push_thread()
        try:
            logger.info('Never gonna give you up')
            logger.warn('Aha!')
            logger.warn('Moar!')
            logger.error('Pure hate!')
        finally:
            handler.pop_thread()

        self.assertEqual(test_handler.formatted_records, [
            '[WARNING] Test: Aha!',
            '[WARNING] Test: Moar!',
            '[ERROR] Test: Pure hate!'
        ])