def publish_or_dump(self, default_logger: Optional[logging.Logger] = None, warn_not_ready: bool = True) -> None: # pylint: disable=import-outside-toplevel; to avoid cyclic imports from sdcm.sct_events.events_device import get_events_main_device if not self._ready_to_publish: if warn_not_ready: LOGGER.warning( "[SCT internal warning] %s is not ready to be published", self) return try: proc = get_events_main_device( _registry=self._events_processes_registry) except RuntimeError: LOGGER.exception("Unable to get events main device") proc = None if proc: if proc.is_alive(): self.publish() else: from sdcm.sct_events.file_logger import get_events_logger get_events_logger(_registry=self._events_processes_registry ).write_event(self) elif default_logger: default_logger.error(str(self)) self._ready_to_publish = False
def publish_or_dump(self, default_logger: Optional[logging.Logger] = None) -> None: # pylint: disable=import-outside-toplevel; to avoid cyclic imports from sdcm.sct_events.events_device import get_events_main_device if not self._ready_to_publish: LOGGER.warning("[SCT internal warning] %s is not ready to be published", self) return if proc := get_events_main_device(_registry=self._events_processes_registry): if proc.is_alive(): self.publish() else: from sdcm.sct_events.file_logger import get_events_logger get_events_logger(_registry=self._events_processes_registry).write_event(self)
def test_file_logger(self): start_events_logger(_registry=self.events_processes_registry) file_logger = get_events_logger(_registry=self.events_processes_registry) time.sleep(EVENTS_SUBSCRIBERS_START_DELAY) try: self.assertIsInstance(file_logger, EventsFileLogger) self.assertTrue(file_logger.is_alive()) self.assertEqual(file_logger._registry, self.events_main_device._registry) self.assertEqual(file_logger._registry, self.events_processes_registry) event_normal = SpotTerminationEvent(node="n1", message="m1") event_normal.severity = Severity.NORMAL event_warning = SpotTerminationEvent(node="n2", message="m2") event_warning.severity = Severity.WARNING event_error = SpotTerminationEvent(node="n3", message="m3") event_error.severity = Severity.ERROR event_critical = SpotTerminationEvent(node="n4", message="m4") event_critical.severity = Severity.CRITICAL with self.wait_for_n_events(file_logger, count=10, timeout=3): self.events_main_device.publish_event(event_normal) self.events_main_device.publish_event(event_warning) self.events_main_device.publish_event(event_warning) self.events_main_device.publish_event(event_error) self.events_main_device.publish_event(event_error) self.events_main_device.publish_event(event_error) self.events_main_device.publish_event(event_critical) self.events_main_device.publish_event(event_critical) self.events_main_device.publish_event(event_critical) self.events_main_device.publish_event(event_critical) self.assertEqual(self.events_main_device.events_counter, file_logger.events_counter) summary = get_logger_event_summary(_registry=self.events_processes_registry) self.assertDictEqual(summary, {Severity.NORMAL.name: 1, Severity.WARNING.name: 2, Severity.ERROR.name: 3, Severity.CRITICAL.name: 4, }) grouped = get_events_grouped_by_category(_registry=self.events_processes_registry) self.assertEqual(len(grouped[Severity.NORMAL.name]), 1) self.assertEqual(len(grouped[Severity.WARNING.name]), 2) self.assertEqual(len(grouped[Severity.ERROR.name]), 3) self.assertEqual(len(grouped[Severity.CRITICAL.name]), 4) finally: file_logger.stop(timeout=1)
def setUp(self) -> None: self.setup_events_processes(events_device=False, events_main_device=True, registry_patcher=False) start_events_logger(_registry=self.events_processes_registry) self.file_logger = get_events_logger( _registry=self.events_processes_registry) time.sleep(EVENTS_SUBSCRIBERS_START_DELAY) self.assertIsInstance(self.file_logger, EventsFileLogger) self.assertTrue(self.file_logger.is_alive()) self.assertEqual(self.file_logger._registry, self.events_main_device._registry) self.assertEqual(self.file_logger._registry, self.events_processes_registry)
def get_events_logger(cls): return get_events_logger(_registry=cls.events_processes_registry)