diff --git a/monai/apps/utils.py b/monai/apps/utils.py index 0c998146a3..e4fec7d190 100644 --- a/monai/apps/utils.py +++ b/monai/apps/utils.py @@ -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 diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 4251da0b6f..884723ed68 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -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" @@ -1969,7 +1968,7 @@ def create_workflow( workflow_ = workflow_class(**_args) workflow_.initialize() - + _log_input_summary(tag="run", args=_args) return workflow_ diff --git a/monai/bundle/workflows.py b/monai/bundle/workflows.py index d728d7d930..ccdb08e208 100644 --- a/monai/bundle/workflows.py +++ b/monai/bundle/workflows.py @@ -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)