Skip to content

Commit

Permalink
Enhance logging logic in ConfigWorkflow (#7745)
Browse files Browse the repository at this point in the history
When using nvflare with monai bundle, the logging logic may be overrided
by the logging logic in the bundle.
Add an option to disable logging logic in the bundle.

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
  • Loading branch information
KumoLiu and ericspod authored May 8, 2024
1 parent ecaf5a1 commit 32b7754
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
9 changes: 6 additions & 3 deletions monai/bundle/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ class ConfigWorkflow(BundleWorkflow):
logging_file: config file for `logging` module in the program. for more details:
https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig.
If None, default to "configs/logging.conf", which is commonly used for bundles in MONAI model zoo.
If False, the logging logic for the bundle will not be modified.
init_id: ID name of the expected config expression to initialize before running, default to "initialize".
allow a config to have no `initialize` logic and the ID.
run_id: ID name of the expected config expression to run, default to "run".
Expand Down Expand Up @@ -278,7 +279,7 @@ def __init__(
self,
config_file: str | Sequence[str],
meta_file: str | Sequence[str] | None = None,
logging_file: str | None = None,
logging_file: str | bool | None = None,
init_id: str = "initialize",
run_id: str = "run",
final_id: str = "finalize",
Expand Down Expand Up @@ -307,15 +308,17 @@ def __init__(
super().__init__(workflow_type=workflow_type, meta_file=meta_file, properties_path=properties_path)
self.config_root_path = config_root_path
logging_file = str(self.config_root_path / "logging.conf") if logging_file is None else logging_file
if logging_file is not None:
if logging_file is False:
logger.warn(f"Logging file is set to {logging_file}, skipping logging.")
else:
if not os.path.isfile(logging_file):
if logging_file == str(self.config_root_path / "logging.conf"):
logger.warn(f"Default logging file in {logging_file} does not exist, skipping logging.")
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(logging_file, disable_existing_loggers=False)
fileConfig(str(logging_file), disable_existing_loggers=False)

self.parser = ConfigParser()
self.parser.read_config(f=config_file)
Expand Down
14 changes: 9 additions & 5 deletions monai/fl/client/monai_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ def initialize(self, extra=None):
Args:
extra: Dict with additional information that should be provided by FL system,
i.e., `ExtraItems.CLIENT_NAME` and `ExtraItems.APP_ROOT`.
i.e., `ExtraItems.CLIENT_NAME`, `ExtraItems.APP_ROOT` and `ExtraItems.LOGGING_FILE`.
You can diable the logging logic in the monai bundle by setting {ExtraItems.LOGGING_FILE} to False.
"""
if extra is None:
extra = {}
self.client_name = extra.get(ExtraItems.CLIENT_NAME, "noname")
logging_file = extra.get(ExtraItems.LOGGING_FILE, None)
self.logger.info(f"Initializing {self.client_name} ...")

# FL platform needs to provide filepath to configuration files
Expand All @@ -149,7 +151,7 @@ def initialize(self, extra=None):
if self.workflow is None:
config_train_files = self._add_config_files(self.config_train_filename)
self.workflow = ConfigWorkflow(
config_file=config_train_files, meta_file=None, logging_file=None, workflow_type="train"
config_file=config_train_files, meta_file=None, logging_file=logging_file, workflow_type="train"
)
self.workflow.initialize()
self.workflow.bundle_root = self.bundle_root
Expand Down Expand Up @@ -412,13 +414,15 @@ def initialize(self, extra=None):
Args:
extra: Dict with additional information that should be provided by FL system,
i.e., `ExtraItems.CLIENT_NAME` and `ExtraItems.APP_ROOT`.
i.e., `ExtraItems.CLIENT_NAME`, `ExtraItems.APP_ROOT` and `ExtraItems.LOGGING_FILE`.
You can diable the logging logic in the monai bundle by setting {ExtraItems.LOGGING_FILE} to False.
"""
self._set_cuda_device()
if extra is None:
extra = {}
self.client_name = extra.get(ExtraItems.CLIENT_NAME, "noname")
logging_file = extra.get(ExtraItems.LOGGING_FILE, None)
timestamp = time.strftime("%Y%m%d_%H%M%S")
self.logger.info(f"Initializing {self.client_name} ...")
# FL platform needs to provide filepath to configuration files
Expand All @@ -434,7 +438,7 @@ def initialize(self, extra=None):
self.train_workflow = ConfigWorkflow(
config_file=config_train_files,
meta_file=None,
logging_file=None,
logging_file=logging_file,
workflow_type="train",
**self.train_kwargs,
)
Expand All @@ -459,7 +463,7 @@ def initialize(self, extra=None):
self.eval_workflow = ConfigWorkflow(
config_file=config_eval_files,
meta_file=None,
logging_file=None,
logging_file=logging_file,
workflow_type=self.eval_workflow_name,
**self.eval_kwargs,
)
Expand Down
1 change: 1 addition & 0 deletions monai/fl/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ExtraItems(StrEnum):
CLIENT_NAME = "fl_client_name"
APP_ROOT = "fl_app_root"
STATS_SENDER = "fl_stats_sender"
LOGGING_FILE = "logging_file"


class FlPhase(StrEnum):
Expand Down

0 comments on commit 32b7754

Please sign in to comment.