Skip to content

Commit

Permalink
fix modelzoo#658, part of Project-MONAI#7513
Browse files Browse the repository at this point in the history
Signed-off-by: YunLiu <[email protected]>
  • Loading branch information
KumoLiu committed Oct 11, 2024
1 parent 76ef9f4 commit a094441
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
27 changes: 17 additions & 10 deletions monai/apps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,27 @@ def get_logger(
"""
Get a `module_name` logger with the specified format and date format.
By default, the logger will print to `stdout` at the INFO level.
If `module_name` is `None`, return the root logger.
`fmt` and `datafmt` are passed to a `logging.Formatter` object
- If `module_name` is None, the root logger is returned.
- `fmt` and `datefmt` are passed to `logging.Formatter` for formatting.
(https://docs.python.org/3/library/logging.html#formatter-objects).
`logger_handler` can be used to add an additional handler.
- If `logger_handler` is provided, it will be added to the logger.
"""
adds_stdout_handler = module_name is not None and module_name not in logging.root.manager.loggerDict
# Retrieve or create a logger for the module
logger = logging.getLogger(module_name)
logger.propagate = False
logger.setLevel(logging.INFO)
if adds_stdout_handler: # don't add multiple stdout or add to the root
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter(fmt=fmt, datefmt=datefmt)
handler.setFormatter(formatter)
logger.addHandler(handler)

# Check if stdout handler should be added (avoid adding multiple times)
if module_name and module_name not in logging.root.manager.loggerDict:
if fmt is not None or datefmt is not None:
stdout_handler = next(
(h for h in logging.root.handlers if isinstance(h, logging.StreamHandler)), None
)
if stdout_handler:
# Set the formatter if StreamHandler exists
formatter = logging.Formatter(fmt=fmt, datefmt=datefmt)
stdout_handler.setFormatter(formatter)

# Add custom handler if provided
if logger_handler is not None:
logger.addHandler(logger_handler)
return logger
Expand Down
3 changes: 1 addition & 2 deletions monai/bundle/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1945,7 +1945,6 @@ def create_workflow(
"""
_args = update_kwargs(args=args_file, workflow_name=workflow_name, config_file=config_file, **kwargs)
_log_input_summary(tag="run", args=_args)
(workflow_name, config_file) = _pop_args(
_args, workflow_name=ConfigWorkflow, config_file=None
) # the default workflow name is "ConfigWorkflow"
Expand All @@ -1969,7 +1968,7 @@ def create_workflow(
workflow_ = workflow_class(**_args)

workflow_.initialize()

_log_input_summary(tag="run", args=_args)
return workflow_


Expand Down
2 changes: 1 addition & 1 deletion monai/bundle/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ def __init__(
else:
raise FileNotFoundError(f"Cannot find the logging config file: {logging_file}.")
else:
logger.info(f"Setting logging properties based on config: {logging_file}.")
fileConfig(str(logging_file), disable_existing_loggers=False)
logger.info(f"Setting logging properties based on config: {logging_file}.")

self.parser = ConfigParser()
self.parser.read_config(f=config_file)
Expand Down

0 comments on commit a094441

Please sign in to comment.