Skip to content

Commit

Permalink
Run the root logger handler fix on uninitialised loggers only
Browse files Browse the repository at this point in the history
Adds a more detailed explanation in comments
  • Loading branch information
qizhuli committed Jul 7, 2021
1 parent cc4fccf commit b203441
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions mmcv/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ def get_logger(name, log_file=None, log_level=logging.INFO, file_mode='w'):
logging.Logger: The expected logger.
"""
logger = logging.getLogger(name)

# set the root logger's StreamHandler, if any, to log at the ERROR level
for handler in logger.root.handlers:
if type(handler) is logging.StreamHandler:
handler.setLevel(logging.ERROR)

if name in logger_initialized:
return logger
# handle hierarchical names
Expand All @@ -43,6 +37,17 @@ def get_logger(name, log_file=None, log_level=logging.INFO, file_mode='w'):
if name.startswith(logger_name):
return logger

# handle duplicate logs to the console
# Starting in 1.8.0, PyTorch DDP attaches a StreamHandler <stderr> (NOTSET)
# to the root logger. As logger.propagate is True by default, this root
# level handler causes logging messages from rank>0 processes to
# unexpectedly show up on the console, creating much unwanted clutter.
# To fix this issue, we set the root logger's StreamHandler, if any, to log
# at the ERROR level.
for handler in logger.root.handlers:
if type(handler) is logging.StreamHandler:
handler.setLevel(logging.ERROR)

stream_handler = logging.StreamHandler()
handlers = [stream_handler]

Expand Down

0 comments on commit b203441

Please sign in to comment.