def setUp(self):
        BasicPySlotCase.setUp(self)

        # Create the logger
        level = logging.DEBUG
        self.logger = logging.getLogger(name='test')
        self.logger.setLevel(level)

        # Create the handler
        self.leap_handler = LeapLogHandler()
        self.leap_handler.setLevel(level)
        self.logger.addHandler(self.leap_handler)
class LeapLogHandlerTest(BaseLeapTest, BasicPySlotCase):
    """
    LeapLogHandlerTest's tests.
    """
    def _callback(self, *args):
        """
        Simple callback to track if a signal was emitted.
        """
        self.called = True
        self.emitted_msg = args[0][LeapLogHandler.MESSAGE_KEY]

    def setUp(self):
        BasicPySlotCase.setUp(self)

        # Create the logger
        level = logging.DEBUG
        self.logger = logging.getLogger(name='test')
        self.logger.setLevel(level)

        # Create the handler
        self.leap_handler = LeapLogHandler()
        self.leap_handler.setLevel(level)
        self.logger.addHandler(self.leap_handler)

    def tearDown(self):
        BasicPySlotCase.tearDown(self)
        try:
            self.leap_handler.new_log.disconnect()
        except Exception:
            pass

    def test_history_starts_empty(self):
        self.assertEqual(self.leap_handler.log_history, [])

    def test_one_log_captured(self):
        self.logger.debug('test')
        self.assertEqual(len(self.leap_handler.log_history), 1)

    def test_history_records_order(self):
        self.logger.debug('test 01')
        self.logger.debug('test 02')
        self.logger.debug('test 03')

        logs = []
        for message in self.leap_handler.log_history:
            logs.append(message[LeapLogHandler.RECORD_KEY].msg)

        self.assertIn('test 01', logs)
        self.assertIn('test 02', logs)
        self.assertIn('test 03', logs)

    def test_history_messages_order(self):
        self.logger.debug('test 01')
        self.logger.debug('test 02')
        self.logger.debug('test 03')

        logs = []
        for message in self.leap_handler.log_history:
            logs.append(message[LeapLogHandler.MESSAGE_KEY])

        self.assertIn('test 01', logs[0])
        self.assertIn('test 02', logs[1])
        self.assertIn('test 03', logs[2])

    def test_emits_signal(self):
        log_format = '%(name)s - %(levelname)s - %(message)s'
        formatter = logging.Formatter(log_format)
        get_format = Mock(return_value=formatter)
        self.leap_handler._handler._get_format = get_format

        self.leap_handler.new_log.connect(self._callback)
        self.logger.debug('test')

        expected_log_msg = "test - DEBUG - test"

        # signal emitted
        self.assertTrue(self.called)

        # emitted message
        self.assertEqual(self.emitted_msg, expected_log_msg)

        # Mock called
        self.assertTrue(get_format.called)
예제 #3
0
def create_logger(debug=False, logfile=None, replace_stdout=True):
    """
    Create the logger and attach the handlers.

    :param debug: the level of the messages that we should log
    :type debug: bool
    :param logfile: the file name of where we should to save the logs
    :type logfile: str
    :return: the new logger with the attached handlers.
    :rtype: logging.Logger
    """
    # TODO: get severity from command line args
    if debug:
        level = logging.DEBUG
    else:
        level = logging.WARNING

    # Create logger and formatter
    logger = logging.getLogger(name='leap')
    logger.setLevel(level)
    formatter = logging.Formatter(LOG_FORMAT)

    # Console handler
    try:
        import coloredlogs
        console = coloredlogs.ColoredStreamHandler(level=level)
    except ImportError:
        console = logging.StreamHandler()
        console.setLevel(level)
        console.setFormatter(formatter)
        using_coloredlog = False
    else:
        using_coloredlog = True

    if using_coloredlog:
        replace_stdout = False

    silencer = SelectiveSilencerFilter()
    console.addFilter(silencer)
    logger.addHandler(console)
    logger.debug('Console handler plugged!')

    # LEAP custom handler
    leap_handler = LeapLogHandler()
    leap_handler.setLevel(level)
    leap_handler.addFilter(silencer)
    logger.addHandler(leap_handler)
    logger.debug('Leap handler plugged!')

    # File handler
    if logfile is not None:
        logger.debug('Setting logfile to %s ', logfile)
        fileh = logging.FileHandler(logfile)
        fileh.setLevel(logging.DEBUG)
        fileh.setFormatter(formatter)
        fileh.addFilter(silencer)
        logger.addHandler(fileh)
        logger.debug('File handler plugged!')

    if replace_stdout:
        replace_stdout_stderr_with_logging(logger)

    return logger