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")
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")
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")
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")
def test_non_verbose_when_info(): # Given collectd = mock_collectd() handler = CollectdLogHandler(collectd=collectd) handler.verbose = False logger = logging.Logger('some_logger') logger.addHandler(handler) # When debug is called logger.debug('Say cheese: %s %d', 'string', 10) # Then debug hook is called collectd.debug.assert_called_once_with('Say cheese: string 10') collectd.info.assert_not_called()
def register(cls): assert collectd is not None LOG.info("Register plugin: %s", cls) log_handler = CollectdLogHandler(collectd=collectd) log_handler.setFormatter(logging.Formatter(LOG_FORMAT)) logging.getLogger('collectd_pandas').addHandler(log_handler) instance = cls() collectd.register_config(instance.configure) collectd.register_init(instance.initialize) collectd.register_write(instance.write) LOG.info("Plugin registered as: %r.", instance) return instance
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) ])
def test_fatal_from_logger(): # Given collectd = mock_collectd() handler = CollectdLogHandler(collectd=collectd) logger = logging.Logger('some_logger') logger.addHandler(handler) # When fatal is called logger.fatal('Say cheese: %s %d', 'string', 10) # Then error hook is called collectd.error.assert_called_once_with('Say cheese: string 10')
def test_warning_from_logger(): # Given collectd = mock_collectd() handler = CollectdLogHandler(collectd=collectd) logger = logging.Logger('some_logger') logger.addHandler(handler) # When warning is called logger.warning('Say cheese: %s %d', 'string', 10) # Then warning hook is called collectd.warning.assert_called_once_with('Say cheese: string 10')
def test_registered_hooks_when_init(self): collectd = mock_collectd() # When CollectdLogHandler is created handler = CollectdLogHandler(collectd=collectd) # Then collectd logging hooks are registered # pylint: disable=protected-access self.assertEqual( { logging.DEBUG: collectd.debug, logging.INFO: collectd.info, logging.WARNING: collectd.warning, logging.ERROR: collectd.error }, handler.priority_map)
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"))