Exemple #1
0
    def test_error_when_emit():
        # Given
        collectd = mock_collectd()
        handler = CollectdLogHandler(collectd=collectd)
        record = make_record(msg="message", level=logging.ERROR)

        # When an error record is emitted
        handler.emit(record=record)

        # Then error hook is called
        collectd.error.assert_called_once_with("message")
Exemple #2
0
    def test_warning_when_emit():
        # Given
        collectd = mock_collectd()
        handler = CollectdLogHandler(collectd=collectd)
        record = make_record(msg="message", level=logging.WARNING)

        # When a warning record is emitted
        handler.emit(record=record)

        # Then info warning is called
        collectd.warning.assert_called_once_with("message")
Exemple #3
0
    def test_info_when_emit():
        # Given
        collectd = mock_collectd()
        handler = CollectdLogHandler(collectd=collectd)
        record = make_record(msg="message", level=logging.INFO)

        # When an info record is emitted
        handler.emit(record=record)

        # Then info hook is called
        collectd.info.assert_called_once_with("message")
Exemple #4
0
    def test_debug_when_emit():

        collectd = mock_collectd()

        # Given
        handler = CollectdLogHandler(collectd=collectd)
        record = make_record(msg="message", level=logging.DEBUG)

        # When a debug record is emitted
        handler.emit(record=record)

        # Then debug hook is called
        collectd.debug.assert_called_once_with("message")
Exemple #5
0
    def test_long_message_when_emit():
        # Given
        collectd = mock_collectd()
        long_message = "LONG " * 20 + "MESSAGE."
        handler = CollectdLogHandler(collectd=collectd)
        handler.max_message_length = 10
        record = make_record(msg=long_message)

        # When a long message is emitted
        handler.emit(record=record)

        # Then info hook is called n times with split message
        collectd.info.assert_has_calls([
            call(long_message[i:i + 10])
            for i in range(0, len(long_message), 10)
        ])
Exemple #6
0
    def test_formatting_error():
        # Given
        collectd = mock_collectd()
        handler = CollectdLogHandler(collectd=collectd)
        formatter = MagicMock(autospec=logging.Formatter)
        formatter.format.side_effect = RuntimeError
        handler.setFormatter(formatter)
        record = make_record(msg="message %s", level=logging.INFO)

        # When a warning record is emitted
        handler.emit(record=record)

        # Then info warning is called
        collectd.error.assert_called_once_with(
            wildcard("Error emitting message:\n"
                     "Traceback (most recent call last):\n"
                     "*\n"
                     "RuntimeError\n"))