Shortcuts

MMLogger

class mmengine.logging.MMLogger(name, logger_name='mmengine', log_file=None, log_level='INFO', file_mode='w', distributed=False, file_handler_cfg=None)[源代码]

Formatted logger used to record messages.

MMLogger can create formatted logger to log message with different log levels and get instance in the same way as ManagerMixin. MMLogger has the following features:

  • Distributed log storage, MMLogger can choose whether to save log of different ranks according to log_file.

  • Message with different log levels will have different colors and format when displayed on terminal.

备注

  • The name of logger and the instance_name of MMLogger could be different. We can only get MMLogger instance by MMLogger.get_instance but not logging.getLogger. This feature ensures MMLogger will not be incluenced by third-party logging config.

  • Different from logging.Logger, MMLogger will not log warning or error message without Handler.

示例

>>> logger = MMLogger.get_instance(name='MMLogger',
>>>                                logger_name='Logger')
>>> # Although logger has name attribute just like `logging.Logger`
>>> # We cannot get logger instance by `logging.getLogger`.
>>> assert logger.name == 'Logger'
>>> assert logger.instance_name = 'MMLogger'
>>> assert id(logger) != id(logging.getLogger('Logger'))
>>> # Get logger that do not store logs.
>>> logger1 = MMLogger.get_instance('logger1')
>>> # Get logger only save rank0 logs.
>>> logger2 = MMLogger.get_instance('logger2', log_file='out.log')
>>> # Get logger only save multiple ranks logs.
>>> logger3 = MMLogger.get_instance('logger3', log_file='out.log',
>>>                                 distributed=True)
参数:
  • name (str) – Global instance name.

  • logger_name (str) – name attribute of Logging.Logger instance. If logger_name is not defined, defaults to ‘mmengine’.

  • log_file (str, optional) – The log filename. If specified, a FileHandler will be added to the logger. Defaults to None.

  • log_level (str) – The log level of the handler. Defaults to ‘INFO’. If log level is ‘DEBUG’, distributed logs will be saved during distributed training.

  • file_mode (str) – The file mode used to open log file. Defaults to ‘w’.

  • distributed (bool) – Whether to save distributed logs, Defaults to false.

  • file_handler_cfg (dict, optional) –

    Configuration of file handler. Defaults to None. If file_handler_cfg is not specified, logging.FileHandler will be used by default. If it is specified, the type key should be set. It can be RotatingFileHandler, TimedRotatingFileHandler, WatchedFileHandler or other file handlers, and the remaining fields will be used to build the handler.

    示例

    >>> file_handler_cfg = dict(
    >>>    type='TimedRotatingFileHandler',
    >>>    when='MIDNIGHT',
    >>>    interval=1,
    >>>    backupCount=365)
    

    New in version 0.9.0.

callHandlers(record)[源代码]

Pass a record to all relevant handlers.

Override callHandlers method in logging.Logger to avoid multiple warning messages in DDP mode. Loop through all handlers of the logger instance and its parents in the logger hierarchy. If no handler was found, the record will not be output.

参数:

record (LogRecord) – A LogRecord instance contains logged message.

返回类型:

None

classmethod get_current_instance()[源代码]

Get latest created MMLogger instance.

MMLogger can call get_current_instance() before any instance has been created, and return a logger with the instance name “mmengine”.

返回:

Configured logger instance.

返回类型:

MMLogger

setLevel(level)[源代码]

Set the logging level of this logger.

If logging.Logger.selLevel is called, all logging.Logger instances managed by logging.Manager will clear the cache. Since MMLogger is not managed by logging.Manager anymore, MMLogger should override this method to clear caches of all MMLogger instance which is managed by ManagerMixin.

level must be an int or a str.