Fix: prevent StorageManager from interfering with root logger (#1405)

This commit is contained in:
Joseph Vincent 2025-05-20 18:12:10 -04:00
parent 5a5ff31a5b
commit 449ae18f00
3 changed files with 32 additions and 3 deletions

View File

@ -41,5 +41,9 @@ def initialize(
Logger.manager.loggerClass = _Logger
if logging_config is not None:
# Use deepcopy since Python's logging infrastructure might modify the dict
logging.config.dictConfig(deepcopy(dict(logging_config)))
root_logger = logging.getLogger()
#added a check to avoid calling dictConfig
#if the root user has already set up their own logging.
#prevents ClearML from closing their handlers.
if not root_logger.handlers:
logging.config.dictConfig(deepcopy(dict(logging_config)))

View File

@ -164,8 +164,10 @@ class LoggerRoot(object):
loggers = [logging.getLogger()] + list(logging.Logger.manager.loggerDict.values())
for logger in loggers:
handlers = getattr(logger, "handlers", [])
for handler in handlers:
# Iterate over a copy to avoid modifying the list while removing handlers
for handler in handlers[:]:
if isinstance(handler, ClearmlLoggerHandler):
handler.close()
logger.removeHandler(handler)

23
test_storage_manager.py Normal file
View File

@ -0,0 +1,23 @@
import logging
from rich.logging import RichHandler
from clearml.storage.manager import StorageManager
def test_storage_manager_does_not_break_root_logger(tmp_path):
log_file = tmp_path / "test.log"
local_file = tmp_path / "dummy.txt"
local_file.write_text("hello")
handlers = [
RichHandler(rich_tracebacks=True),
logging.FileHandler(log_file, mode='w')
]
logging.basicConfig(level=logging.INFO, format="%(message)s", handlers=handlers, force=True)
logging.info("before download")
StorageManager.get_local_copy(str(local_file))
logging.info("after download")
with open(log_file) as f:
contents = f.read()
assert "before download" in contents, "'before download' missing from log file"
assert "after download" in contents, "'after download' missing from log file"