diff --git a/mmengine/logging/logger.py b/mmengine/logging/logger.py index 059110c4..056aec29 100644 --- a/mmengine/logging/logger.py +++ b/mmengine/logging/logger.py @@ -19,18 +19,24 @@ class MMFormatter(logging.Formatter): Args: color (bool): Whether to use colorful format. filehandler is not allowed to use color format, otherwise it will be garbled. + blink (bool): Whether to blink the ``INFO`` and ``DEBUG`` logging + level. + **kwargs: Keyword arguments passed to + :meth:`logging.Formatter.__init__`. """ _color_mapping: dict = dict( ERROR='red', WARNING='yellow', INFO='white', DEBUG='green') - def __init__(self, color: bool = True, **kwargs): - + def __init__(self, color: bool = True, blink: bool = False, **kwargs): super().__init__(**kwargs) + assert not (not color and blink), ( + 'blink should only be available when color is True') # Get prefix format according to color. - error_prefix = self._get_prefix('ERROR', color) - warn_prefix = self._get_prefix('WARNING', color) - info_prefix = self._get_prefix('INFO', color) - debug_prefix = self._get_prefix('DEBUG', color) + error_prefix = self._get_prefix('ERROR', color, blink=True) + warn_prefix = self._get_prefix('WARNING', color, blink=True) + info_prefix = self._get_prefix('INFO', color, blink) + debug_prefix = self._get_prefix('DEBUG', color, blink) + # Config output format. self.err_format = (f'%(asctime)s - %(name)s - {error_prefix} - ' '%(pathname)s - %(funcName)s - %(lineno)d - ' @@ -42,21 +48,22 @@ class MMFormatter(logging.Formatter): self.debug_format = (f'%(asctime)s - %(name)s - {debug_prefix} - %(' 'message)s') - def _get_prefix(self, level: str, color: bool) -> str: + def _get_prefix(self, level: str, color: bool, blink=False) -> str: """Get the prefix of the target log level. Args: level (str): log level. color (bool): Whether to get colorful prefix. + blink (bool): Whether the prefix will blink. Returns: str: The plain or colorful prefix. """ if color: - prefix = colored( - level, - self._color_mapping[level], - attrs=['blink', 'underline']) + attrs = ['underline'] + if blink: + attrs.append('blink') + prefix = colored(level, self._color_mapping[level], attrs=attrs) else: prefix = level return prefix