def test_should_print_with_missing_replica(self): printer = UidPrinter() module_name, module_kind = 'test-x', 'TestX' group_name = 'GroupG1' module = LoggerParts.Module(module_name, module_kind) group = LoggerParts.Group(group_name) output = printer.flush(colors=False, group=group, module=module) assert output.find(module_name) != -1 assert output.find(module_kind) != -1 assert output.find(group_name) != -1 assert re.search(self.COLOR_REGEXP, output) is None
def test_should_ignore_extra_arguments(self): printer = UidPrinter() module_name, module_kind = 'test-x', 'TestX' group_name, replica = 'GroupG1', 222 module = LoggerParts.Module(module_name, module_kind) group = LoggerParts.Group(group_name, replica) output_base = printer.flush(colors=True, group=group, module=module) output_extra = printer.flush(colors=True, group=group, module=module, extra=[1, 2, 3]) assert output_base == output_extra
def test_should_print_complete_uid(self): printer = UidPrinter() module_name, module_kind = 'test-x', 'TestX' group_name, replica = 'GroupG1', 222 module = LoggerParts.Module(module_name, module_kind) group = LoggerParts.Group(group_name, replica) output = printer.flush(colors=False, group=group, module=module) assert output.find(module_name) != -1 assert output.find(module_kind) != -1 assert output.find(group_name) != -1 assert output.find(str(replica)) != -1 assert re.search(self.COLOR_REGEXP, output) is None
def test_should_print_level_correctly(self, level, expected_color): printer = LevelPrinter() level = LoggerParts.Level(level) output = printer.flush(colors=True, level=level) level_msg = (f'{LevelPrinter.LEVEL_START_MARKER}' f'{level.value.name}{LevelPrinter.LEVEL_END_MARKER}') assert level_msg in output assert expected_color in output
def test_should_log_non_info_level_without_color_if_set(self, level): printer = MessagePrinter() level = LoggerParts.Level(level) base = 'TEST' output = printer.flush(colors=False, msg=base, is_event=False, level=level) assert re.search(self.COLOR_REGEXP, output) is None
def test_should_ignore_extra_arguments(self): printer = ReplicaPrinter() group, replica = 'GroupG1', 20 part = LoggerParts.Group(group, replica) output_base = printer.flush(colors=False, group=part) output_extra = printer.flush(colors=False, group=part, extra=[1, 2, 3]) assert output_base == output_extra
def test_should_print_replica(self): printer = ReplicaPrinter() group, replica = 'GroupG1', 20 part = LoggerParts.Group(group, replica) output = printer.flush(colors=False, group=part) assert output.find(str(replica)) != -1 assert re.search(self.COLOR_REGEXP, output) is None
def test_should_print_request(self): printer = RequestPrinter() request = 'Request(variable=5)' part = LoggerParts.Request(request) output = printer.flush(colors=False, request=part) assert output.find(request) != -1 assert re.search(self.COLOR_REGEXP, output) is None
def test_should_print_group(self): printer = GroupPrinter() group = 'GroupG1' part = LoggerParts.Group(group) output = printer.flush(colors=False, group=part) assert output.find(group) != -1 assert re.search(self.COLOR_REGEXP, output) is None
def test_should_log_non_info_level_in_color(self, level, expected_color): printer = MessagePrinter() level = LoggerParts.Level(level) base = 'TEST' message_formatted = expected_color + base + Fore.RESET output = printer.flush(colors=True, msg=base, is_event=False, level=level) assert output.find(message_formatted) != -1
def test_should_print_module(self): printer = ModulePrinter() name, kind = 'test-x', 'TestX' part = LoggerParts.Module(name, kind) output = printer.flush(colors=False, module=part) assert output.find(name) != -1 assert output.find(kind) != -1 assert re.search(self.COLOR_REGEXP, output) is None
def test_should_print_critical_in_color_and_bold(self): printer = MessagePrinter() base = 'TEST' message_formatted = Style.BRIGHT + Fore.RED + base + Fore.RESET output = printer.flush(colors=True, msg=base, is_event=False, level=LoggerParts.Level( LoggerConfig.Level.CRITICAL)) assert output.find(message_formatted) != -1
def test_should_print_pipeline_in_colors(self): printer = PipelinePrinter() name, kind = 'test-x', 'TestX' part = LoggerParts.Pipeline(name, kind) output = printer.flush(colors=True, pipeline=part) assert output.find(name) != -1 assert output.find(kind) != -1 assert re.search(self.COLOR_REGEXP, output) is not None
def test_should_ignore_extra_arguments(self): printer = ModulePrinter() name, kind = 'test-x', 'TestX' part = LoggerParts.Module(name, kind) output_base = printer.flush(colors=False, module=part) output_extra = printer.flush(colors=False, module=part, extra=[1, 2, 3]) assert output_base == output_extra
def test_should_ignore_extra_arguments(self): printer = RequestPrinter() request = 'Request(variable=5)' part = LoggerParts.Request(request) output_base = printer.flush(colors=False, request=part) output_extra = printer.flush(colors=False, request=part, extra=[1, 2, 3]) assert output_base == output_extra
def _prepare_message( config: MagdaLogger.Config, **kwargs, ) -> None: is_event = kwargs.get('is_event', False) level = kwargs.get('level', LoggerParts.Level(LoggerConfig.Level.INFO)) if not config.enable or (not config.log_events and is_event): return message_format = config.format hidden_parts = [] if config.output == MagdaLogger.Config.Output.LOGGING: hidden_parts.append(LoggerConfig.Part.LEVEL) parts = [ MagdaLogger._parts_mapping[key].flush( colors=config.colors, **kwargs, ) for key in message_format if key not in hidden_parts ] message = ' '.join([p for p in parts if p is not None]) if config.output == MagdaLogger.Config.Output.STDOUT: print(message) elif config.output == MagdaLogger.Config.Output.LOGGING: if level.value == LoggerConfig.Level.DEBUG: getLogger('magda.runtime').debug(message) elif level.value == LoggerConfig.Level.ERROR: getLogger('magda.runtime').error(message) elif level.value == LoggerConfig.Level.WARNING: getLogger('magda.runtime').warn(message) elif level.value == LoggerConfig.Level.CRITICAL: getLogger('magda.runtime').critical(message) else: getLogger('magda.runtime').info(message) elif callable(config.output): config.output(message)
def event(self, msg: str) -> None: level = LoggerParts.Level(LoggerConfig.Level.INFO) if self._callback: self._callback(msg=msg, is_event=True, level=level)
def log_message(self, msg: str, level: LoggerConfig.Level) -> None: if self._callback: level = LoggerParts.Level(level) self._callback(msg=msg, is_event=False, level=level)
def test_should_log_level_without_color_if_set(self, level): printer = LevelPrinter() level = LoggerParts.Level(level) output = printer.flush(colors=False, level=level) assert re.search(self.COLOR_REGEXP, output) is None
def test_should_print_critical_level_in_color_and_bold(self): printer = LevelPrinter() message = f'{LevelPrinter.LEVEL_START_MARKER}CRITICAL{LevelPrinter.LEVEL_END_MARKER}' message_formatted = Style.BRIGHT + Fore.RED + message + Fore.RESET output = printer.flush(colors=True, level=LoggerParts.Level(LoggerConfig.Level.CRITICAL)) assert output.find(message_formatted) != -1