From 43ecb0f0a9ef5e408cb62f0983141f67d0fae13a Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Sat, 3 Jun 2023 15:29:23 +0800 Subject: [PATCH 01/10] [Feature] add an interface to rotate log file for MMLogger --- mmengine/logging/logger.py | 43 +++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/mmengine/logging/logger.py b/mmengine/logging/logger.py index cb518187ea..ecfac54a53 100644 --- a/mmengine/logging/logger.py +++ b/mmengine/logging/logger.py @@ -6,6 +6,7 @@ import warnings from getpass import getuser from logging import Logger, LogRecord +from logging.handlers import RotatingFileHandler, TimedRotatingFileHandler from socket import gethostname from typing import Optional, Union @@ -170,6 +171,19 @@ class MMLogger(Logger, ManagerMixin): 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. + log_rotating (dict, optional): The cfg dict of file handler that + rotate log files at a certain point.If `log_rotating=None`, + a ``FileHandler`` that does not rotate log file to the logger. + The dict of log_rotating can only support the type keyword + with the value of `time` or `size`. And the remaining key pairs + are the parameters required by ``logging.handlers.RotatingFileHandler`` + or ``logging.handlers.TimedRotatingFileHandler``. Like: + log_rotating = dict( + type='time', + when='MIDNIGHT', + interval=1, + backupCount=365) + Defaults to None. file_mode (str): The file mode used to open log file. Defaults to 'w'. distributed (bool): Whether to save distributed logs, Defaults to false. @@ -180,6 +194,7 @@ def __init__(self, logger_name='mmengine', log_file: Optional[str] = None, log_level: Union[int, str] = 'INFO', + log_rotating: Optional[dict] = None, file_mode: str = 'w', distributed=False): Logger.__init__(self, logger_name) @@ -223,14 +238,26 @@ def __init__(self, # Save multi-ranks logs if distributed is True. The logs of rank0 # will always be saved. if global_rank == 0 or is_distributed: - # Here, the default behaviour of the official logger is 'a'. - # Thus, we provide an interface to change the file mode to - # the default behaviour. `FileHandler` is not supported to - # have colors, otherwise it will appear garbled. - file_handler = logging.FileHandler(log_file, file_mode) - # `StreamHandler` record year, month, day hour, minute, - # and second timestamp. file_handler will only record logs - # without color to avoid garbled code saved in files. + if log_rotating is not None: + assert 'type' in log_rotating, \ + 'You should specify the `type` of log_rotating.' + log_rotating_type = log_rotating.pop('type') + if log_rotating_type == 'time': + file_handler = TimedRotatingFileHandler(filename=log_file, **log_rotating) + elif log_rotating_type == 'size': + file_handler = RotatingFileHandler(filename=log_file, **log_rotating) + else: + raise ValueError('The type of log_rotating should be `time` or `size`, ' + f'but got {log_rotating_type}.') + else: + # Here, the default behaviour of the official logger is 'a'. + # Thus, we provide an interface to change the file mode to + # the default behaviour. `FileHandler` is not supported to + # have colors, otherwise it will appear garbled. + file_handler = logging.FileHandler(log_file, file_mode) + # `StreamHandler` record year, month, day hour, minute, + # and second timestamp. file_handler will only record logs + # without color to avoid garbled code saved in files. file_handler.setFormatter( MMFormatter(color=False, datefmt='%Y/%m/%d %H:%M:%S')) file_handler.setLevel(log_level) From 5e3c254cee66d3e2078d954407eb0a627a8eafec Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Sat, 3 Jun 2023 15:42:02 +0800 Subject: [PATCH 02/10] a small error --- mmengine/model/base_model/data_preprocessor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmengine/model/base_model/data_preprocessor.py b/mmengine/model/base_model/data_preprocessor.py index 7f8a54c178..1f285aca62 100644 --- a/mmengine/model/base_model/data_preprocessor.py +++ b/mmengine/model/base_model/data_preprocessor.py @@ -293,7 +293,7 @@ def forward(self, data: dict, training: bool = False) -> Union[dict, list]: else: raise TypeError('Output of `cast_data` should be a dict of ' 'list/tuple with inputs and data_samples, ' - f'but got {type(data)}: {data}') + f'but got {type(data)}: {data}') data['inputs'] = batch_inputs data.setdefault('data_samples', None) return data From c1916fc2a66ca4782e47c80780764288c5034ed9 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Thu, 15 Jun 2023 21:43:26 +0800 Subject: [PATCH 03/10] rename log_rotating to file_handler --- mmengine/logging/logger.py | 59 +++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/mmengine/logging/logger.py b/mmengine/logging/logger.py index ecfac54a53..6d1d27689e 100644 --- a/mmengine/logging/logger.py +++ b/mmengine/logging/logger.py @@ -171,18 +171,20 @@ class MMLogger(Logger, ManagerMixin): 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. - log_rotating (dict, optional): The cfg dict of file handler that - rotate log files at a certain point.If `log_rotating=None`, - a ``FileHandler`` that does not rotate log file to the logger. - The dict of log_rotating can only support the type keyword - with the value of `time` or `size`. And the remaining key pairs - are the parameters required by ``logging.handlers.RotatingFileHandler`` + file_handler_cfg (dict, optional): The cfg dict of file handler that + rotate log files at a certain point. If `file_handler_cfg=None`, + a ``logging.FileHandler`` that does not rotate log file would + be added to the logger. The file_handler_cfg can only + support the type keyword with the value of `time` or `size`. + And the remaining key pairs are the parameters required by + ``logging.handlers.RotatingFileHandler`` or ``logging.handlers.TimedRotatingFileHandler``. Like: - log_rotating = dict( - type='time', - when='MIDNIGHT', - interval=1, - backupCount=365) + Examples: + >>> file_handler_cfg = dict( + >>> type='time', + >>> when='MIDNIGHT', + >>> interval=1, + >>> backupCount=365) Defaults to None. file_mode (str): The file mode used to open log file. Defaults to 'w'. distributed (bool): Whether to save distributed logs, Defaults to @@ -194,7 +196,7 @@ def __init__(self, logger_name='mmengine', log_file: Optional[str] = None, log_level: Union[int, str] = 'INFO', - log_rotating: Optional[dict] = None, + file_handler_cfg: Optional[dict] = None, file_mode: str = 'w', distributed=False): Logger.__init__(self, logger_name) @@ -238,22 +240,27 @@ def __init__(self, # Save multi-ranks logs if distributed is True. The logs of rank0 # will always be saved. if global_rank == 0 or is_distributed: - if log_rotating is not None: - assert 'type' in log_rotating, \ - 'You should specify the `type` of log_rotating.' - log_rotating_type = log_rotating.pop('type') - if log_rotating_type == 'time': - file_handler = TimedRotatingFileHandler(filename=log_file, **log_rotating) - elif log_rotating_type == 'size': - file_handler = RotatingFileHandler(filename=log_file, **log_rotating) + if file_handler_cfg is not None: + assert 'type' in file_handler_cfg, \ + 'You should specify the `type` keyword in ' + 'file_handler_cfg.' + file_handler_type = file_handler_cfg.pop('type') + if file_handler_type == 'time': + file_handler = TimedRotatingFileHandler( + filename=log_file, **file_handler_cfg) + elif file_handler_type == 'size': + file_handler = RotatingFileHandler( + filename=log_file, **file_handler_cfg) else: - raise ValueError('The type of log_rotating should be `time` or `size`, ' - f'but got {log_rotating_type}.') + raise ValueError('The type of file_handler ' + 'should be `time` or `size`, but ' + f'got {file_handler_type}.') else: - # Here, the default behaviour of the official logger is 'a'. - # Thus, we provide an interface to change the file mode to - # the default behaviour. `FileHandler` is not supported to - # have colors, otherwise it will appear garbled. + # Here, the default behaviour of the official + # logger is 'a'. Thus, we provide an interface to + # change the file mode to the default behaviour. + # `FileHandler` is not supported to have colors, + # otherwise it will appear garbled. file_handler = logging.FileHandler(log_file, file_mode) # `StreamHandler` record year, month, day hour, minute, # and second timestamp. file_handler will only record logs From ab3df09851ceba75bc7a97951721e3e3d597cdfa Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Wed, 21 Jun 2023 17:17:51 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=A4=96=E9=83=A8file?= =?UTF-8?q?=5Fhandlers=E7=9A=84=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0test=5Ffile=5Fhandlers=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mmengine/logging/logger.py | 57 ++++++++++++++++++++----------- tests/test_logging/test_logger.py | 31 +++++++++++++++++ 2 files changed, 69 insertions(+), 19 deletions(-) diff --git a/mmengine/logging/logger.py b/mmengine/logging/logger.py index 6d1d27689e..b154623e1e 100644 --- a/mmengine/logging/logger.py +++ b/mmengine/logging/logger.py @@ -1,14 +1,14 @@ # Copyright (c) OpenMMLab. All rights reserved. +import inspect import logging import os import os.path as osp import sys import warnings from getpass import getuser -from logging import Logger, LogRecord -from logging.handlers import RotatingFileHandler, TimedRotatingFileHandler +from logging import Logger, LogRecord, handlers from socket import gethostname -from typing import Optional, Union +from typing import Dict, Optional, Union from termcolor import colored @@ -174,14 +174,18 @@ class MMLogger(Logger, ManagerMixin): file_handler_cfg (dict, optional): The cfg dict of file handler that rotate log files at a certain point. If `file_handler_cfg=None`, a ``logging.FileHandler`` that does not rotate log file would - be added to the logger. The file_handler_cfg can only - support the type keyword with the value of `time` or `size`. + be added to the logger. The file_handler_cfg can support the + type keyword with the value of + `BaseRotatingHandler`、`RotatingFileHandler`、 + `TimedRotatingFileHandler` or `WatchedFileHandler`. And the remaining key pairs are the parameters required by - ``logging.handlers.RotatingFileHandler`` - or ``logging.handlers.TimedRotatingFileHandler``. Like: + ``logging.handlers.BaseRotatingHandler``、 + ``logging.handlers.RotatingFileHandler``、 + ``logging.handlers.TimedRotatingFileHandler``、 + or ``logging.handlers.WatchedFileHandler``. Like: Examples: >>> file_handler_cfg = dict( - >>> type='time', + >>> type='TimedRotatingFileHandler', >>> when='MIDNIGHT', >>> interval=1, >>> backupCount=365) @@ -242,19 +246,19 @@ def __init__(self, if global_rank == 0 or is_distributed: if file_handler_cfg is not None: assert 'type' in file_handler_cfg, \ - 'You should specify the `type` keyword in ' - 'file_handler_cfg.' + 'You should specify `type` keyword ' + 'in file_handler_cfg.' file_handler_type = file_handler_cfg.pop('type') - if file_handler_type == 'time': - file_handler = TimedRotatingFileHandler( - filename=log_file, **file_handler_cfg) - elif file_handler_type == 'size': - file_handler = RotatingFileHandler( - filename=log_file, **file_handler_cfg) + logging_file_handlers_map = \ + self._get_logging_file_handlers() + if logging_file_handlers_map.get(file_handler_type, + None) is not None: + file_handler = logging_file_handlers_map[ + file_handler_type]( + filename=log_file, **file_handler_cfg) else: - raise ValueError('The type of file_handler ' - 'should be `time` or `size`, but ' - f'got {file_handler_type}.') + raise ValueError('`logging.handlers` does not ' + f'exist {file_handler_type}') else: # Here, the default behaviour of the official # logger is 'a'. Thus, we provide an interface to @@ -272,6 +276,21 @@ def __init__(self, self.handlers.append(file_handler) self._log_file = log_file + def _get_logging_file_handlers(self) -> Dict: + """Get additional file_handlers in ``logging.handlers``. + + Returns: + Dict: A map of file_handlers. + """ + file_handlers_map = {} + for module_name in dir(handlers): + if module_name.startswith('__'): + continue + _fh = getattr(handlers, module_name) + if inspect.isclass(_fh) and issubclass(_fh, logging.FileHandler): + file_handlers_map[module_name] = _fh + return file_handlers_map + @property def log_file(self): return self._log_file diff --git a/tests/test_logging/test_logger.py b/tests/test_logging/test_logger.py index 8cd35ccc93..5dd264e9e1 100644 --- a/tests/test_logging/test_logger.py +++ b/tests/test_logging/test_logger.py @@ -205,6 +205,37 @@ def test_filter(self, capsys): out, _ = capsys.readouterr() assert 'WARNING' in out + def test_file_handlers(self, tmp_path): + tmp_file = tmp_path / 'tmp_file.log' + fh = None + logger = MMLogger( + name='test_file_handlers', log_file=tmp_file, file_handler_cfg=fh) + assert isinstance(logger.handlers[-1], logging.FileHandler) + fh = dict(type='BaseRotatingHandler', mode='a') + logger = MMLogger( + name='test_file_handlers', log_file=tmp_file, file_handler_cfg=fh) + assert isinstance(logger.handlers[-1], + logging.handlers.BaseRotatingHandler) + fh = dict(type='RotatingFileHandler', maxBytes=1024) + logger = MMLogger( + name='test_file_handlers', log_file=tmp_file, file_handler_cfg=fh) + assert isinstance(logger.handlers[-1], + logging.handlers.RotatingFileHandler) + fh = dict(type='TimedRotatingFileHandler', when='MIDNIGHT') + logger = MMLogger( + name='test_file_handlers', log_file=tmp_file, file_handler_cfg=fh) + assert isinstance(logger.handlers[-1], + logging.handlers.TimedRotatingFileHandler) + fh = dict(type='WatchedFileHandler') + logger = MMLogger( + name='test_file_handlers', log_file=tmp_file, file_handler_cfg=fh) + assert isinstance(logger.handlers[-1], + logging.handlers.WatchedFileHandler) + # `FileHandler` should be closed in Windows, otherwise we cannot + # delete the temporary directory + logging.shutdown() + MMLogger._instance_dict.clear() + @patch('torch.cuda.device_count', lambda: 4) def test_get_device_id(): From aefead18fb882d41d8c1e431c4a5fafa5b61b607 Mon Sep 17 00:00:00 2001 From: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> Date: Wed, 23 Aug 2023 14:44:13 +0800 Subject: [PATCH 05/10] Update logger.py --- mmengine/logging/logger.py | 86 ++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 46 deletions(-) diff --git a/mmengine/logging/logger.py b/mmengine/logging/logger.py index b154623e1e..0079c0df0d 100644 --- a/mmengine/logging/logger.py +++ b/mmengine/logging/logger.py @@ -171,28 +171,25 @@ class MMLogger(Logger, ManagerMixin): 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_handler_cfg (dict, optional): The cfg dict of file handler that - rotate log files at a certain point. If `file_handler_cfg=None`, - a ``logging.FileHandler`` that does not rotate log file would - be added to the logger. The file_handler_cfg can support the - type keyword with the value of - `BaseRotatingHandler`、`RotatingFileHandler`、 - `TimedRotatingFileHandler` or `WatchedFileHandler`. - And the remaining key pairs are the parameters required by - ``logging.handlers.BaseRotatingHandler``、 - ``logging.handlers.RotatingFileHandler``、 - ``logging.handlers.TimedRotatingFileHandler``、 - or ``logging.handlers.WatchedFileHandler``. Like: + 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): Dict to configure 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 + handler. And the remaining key pairs are passed in the constructor + of the handler. + Examples: >>> file_handler_cfg = dict( >>> type='TimedRotatingFileHandler', >>> when='MIDNIGHT', >>> interval=1, >>> backupCount=365) - Defaults to None. - file_mode (str): The file mode used to open log file. Defaults to 'w'. - distributed (bool): Whether to save distributed logs, Defaults to - false. + + `New in version 0.8.5.` """ def __init__(self, @@ -200,9 +197,9 @@ def __init__(self, logger_name='mmengine', log_file: Optional[str] = None, log_level: Union[int, str] = 'INFO', - file_handler_cfg: Optional[dict] = None, file_mode: str = 'w', - distributed=False): + distributed=False, + file_handler_cfg: Optional[dict] = None): Logger.__init__(self, logger_name) ManagerMixin.__init__(self, name) # Get rank in DDP mode. @@ -245,24 +242,21 @@ def __init__(self, # will always be saved. if global_rank == 0 or is_distributed: if file_handler_cfg is not None: - assert 'type' in file_handler_cfg, \ - 'You should specify `type` keyword ' - 'in file_handler_cfg.' + assert 'type' in file_handler_cfg file_handler_type = file_handler_cfg.pop('type') - logging_file_handlers_map = \ - self._get_logging_file_handlers() - if logging_file_handlers_map.get(file_handler_type, - None) is not None: - file_handler = logging_file_handlers_map[ - file_handler_type]( - filename=log_file, **file_handler_cfg) + file_handlers_map = _get_logging_file_handlers() + if file_handler_type in file_handlers_map: + file_handler_cls = file_handlers_map[file_handler_type] + file_handler_cfg.setdefault('mode', file_mode) + file_handler = file_handler_cls( + filename=log_file, **file_handler_cfg) else: raise ValueError('`logging.handlers` does not ' - f'exist {file_handler_type}') + f'contain {file_handler_type}') else: - # Here, the default behaviour of the official + # Here, the default behavior of the official # logger is 'a'. Thus, we provide an interface to - # change the file mode to the default behaviour. + # change the file mode to the default behavior. # `FileHandler` is not supported to have colors, # otherwise it will appear garbled. file_handler = logging.FileHandler(log_file, file_mode) @@ -276,21 +270,6 @@ def __init__(self, self.handlers.append(file_handler) self._log_file = log_file - def _get_logging_file_handlers(self) -> Dict: - """Get additional file_handlers in ``logging.handlers``. - - Returns: - Dict: A map of file_handlers. - """ - file_handlers_map = {} - for module_name in dir(handlers): - if module_name.startswith('__'): - continue - _fh = getattr(handlers, module_name) - if inspect.isclass(_fh) and issubclass(_fh, logging.FileHandler): - file_handlers_map[module_name] = _fh - return file_handlers_map - @property def log_file(self): return self._log_file @@ -450,3 +429,18 @@ def _get_host_info() -> str: warnings.warn(f'Host or user not found: {str(e)}') finally: return host + +def _get_logging_file_handlers(self) -> Dict: + """Get additional file_handlers in ``logging.handlers``. + + Returns: + Dict: A map of file_handlers. + """ + file_handlers_map = {} + for module_name in dir(handlers): + if module_name.startswith('__'): + continue + _fh = getattr(handlers, module_name) + if inspect.isclass(_fh) and issubclass(_fh, logging.FileHandler): + file_handlers_map[module_name] = _fh + return file_handlers_map From a97377c9db97f7a872ce115049a0619047761480 Mon Sep 17 00:00:00 2001 From: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> Date: Wed, 23 Aug 2023 14:49:46 +0800 Subject: [PATCH 06/10] Update logger.py --- mmengine/logging/logger.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/mmengine/logging/logger.py b/mmengine/logging/logger.py index 0079c0df0d..876f68e61d 100644 --- a/mmengine/logging/logger.py +++ b/mmengine/logging/logger.py @@ -248,6 +248,7 @@ def __init__(self, if file_handler_type in file_handlers_map: file_handler_cls = file_handlers_map[file_handler_type] file_handler_cfg.setdefault('mode', file_mode) + file_handler_cfg.setdefault('filename', log_file) file_handler = file_handler_cls( filename=log_file, **file_handler_cfg) else: @@ -260,9 +261,10 @@ def __init__(self, # `FileHandler` is not supported to have colors, # otherwise it will appear garbled. file_handler = logging.FileHandler(log_file, file_mode) - # `StreamHandler` record year, month, day hour, minute, - # and second timestamp. file_handler will only record logs - # without color to avoid garbled code saved in files. + + # `StreamHandler` record year, month, day hour, minute, + # and second timestamp. file_handler will only record logs + # without color to avoid garbled code saved in files. file_handler.setFormatter( MMFormatter(color=False, datefmt='%Y/%m/%d %H:%M:%S')) file_handler.setLevel(log_level) From 930eba5478eacda6fc9634d5627288dc3f1ede76 Mon Sep 17 00:00:00 2001 From: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> Date: Wed, 23 Aug 2023 18:50:07 +0800 Subject: [PATCH 07/10] Update logger.py --- mmengine/logging/logger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmengine/logging/logger.py b/mmengine/logging/logger.py index 876f68e61d..21f523d1b2 100644 --- a/mmengine/logging/logger.py +++ b/mmengine/logging/logger.py @@ -432,7 +432,7 @@ def _get_host_info() -> str: finally: return host -def _get_logging_file_handlers(self) -> Dict: +def _get_logging_file_handlers() -> Dict: """Get additional file_handlers in ``logging.handlers``. Returns: From d26cd56190ee5542fac839e42d451c86e416091f Mon Sep 17 00:00:00 2001 From: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> Date: Wed, 23 Aug 2023 18:56:44 +0800 Subject: [PATCH 08/10] Update logger.py --- mmengine/logging/logger.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mmengine/logging/logger.py b/mmengine/logging/logger.py index 21f523d1b2..d6a74ba96e 100644 --- a/mmengine/logging/logger.py +++ b/mmengine/logging/logger.py @@ -249,8 +249,7 @@ def __init__(self, file_handler_cls = file_handlers_map[file_handler_type] file_handler_cfg.setdefault('mode', file_mode) file_handler_cfg.setdefault('filename', log_file) - file_handler = file_handler_cls( - filename=log_file, **file_handler_cfg) + file_handler = file_handler_cls(**file_handler_cfg) else: raise ValueError('`logging.handlers` does not ' f'contain {file_handler_type}') From 8f74cfb49b65c1dbe288626ff3df5960ce867032 Mon Sep 17 00:00:00 2001 From: HAOCHENYE <21724054@zju.edu.cn> Date: Wed, 30 Aug 2023 20:13:40 +0800 Subject: [PATCH 09/10] Update changelog --- mmengine/logging/logger.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mmengine/logging/logger.py b/mmengine/logging/logger.py index d6a74ba96e..6e0d50fd21 100644 --- a/mmengine/logging/logger.py +++ b/mmengine/logging/logger.py @@ -176,11 +176,11 @@ class MMLogger(Logger, ManagerMixin): false. file_handler_cfg (dict, optional): Dict to configure 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 - handler. And the remaining key pairs are passed in the constructor - of the handler. + ``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. Examples: >>> file_handler_cfg = dict( @@ -188,7 +188,7 @@ class MMLogger(Logger, ManagerMixin): >>> when='MIDNIGHT', >>> interval=1, >>> backupCount=365) - + `New in version 0.8.5.` """ @@ -247,7 +247,6 @@ def __init__(self, file_handlers_map = _get_logging_file_handlers() if file_handler_type in file_handlers_map: file_handler_cls = file_handlers_map[file_handler_type] - file_handler_cfg.setdefault('mode', file_mode) file_handler_cfg.setdefault('filename', log_file) file_handler = file_handler_cls(**file_handler_cfg) else: @@ -431,6 +430,7 @@ def _get_host_info() -> str: finally: return host + def _get_logging_file_handlers() -> Dict: """Get additional file_handlers in ``logging.handlers``. From 394cfd41ea529439d9575c06182b0d8b52317f58 Mon Sep 17 00:00:00 2001 From: HAOCHENYE <21724054@zju.edu.cn> Date: Wed, 30 Aug 2023 20:20:50 +0800 Subject: [PATCH 10/10] Minor refine --- mmengine/logging/logger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmengine/logging/logger.py b/mmengine/logging/logger.py index 6e0d50fd21..481d6bc878 100644 --- a/mmengine/logging/logger.py +++ b/mmengine/logging/logger.py @@ -174,7 +174,7 @@ class MMLogger(Logger, ManagerMixin): 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): Dict to configure file handler. + 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