Skip to content

Commit 32b7754

Browse files
KumoLiuericspod
andauthored
Enhance logging logic in ConfigWorkflow (#7745)
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>
1 parent ecaf5a1 commit 32b7754

3 files changed

Lines changed: 16 additions & 8 deletions

File tree

monai/bundle/workflows.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ class ConfigWorkflow(BundleWorkflow):
239239
logging_file: config file for `logging` module in the program. for more details:
240240
https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig.
241241
If None, default to "configs/logging.conf", which is commonly used for bundles in MONAI model zoo.
242+
If False, the logging logic for the bundle will not be modified.
242243
init_id: ID name of the expected config expression to initialize before running, default to "initialize".
243244
allow a config to have no `initialize` logic and the ID.
244245
run_id: ID name of the expected config expression to run, default to "run".
@@ -278,7 +279,7 @@ def __init__(
278279
self,
279280
config_file: str | Sequence[str],
280281
meta_file: str | Sequence[str] | None = None,
281-
logging_file: str | None = None,
282+
logging_file: str | bool | None = None,
282283
init_id: str = "initialize",
283284
run_id: str = "run",
284285
final_id: str = "finalize",
@@ -307,15 +308,17 @@ def __init__(
307308
super().__init__(workflow_type=workflow_type, meta_file=meta_file, properties_path=properties_path)
308309
self.config_root_path = config_root_path
309310
logging_file = str(self.config_root_path / "logging.conf") if logging_file is None else logging_file
310-
if logging_file is not None:
311+
if logging_file is False:
312+
logger.warn(f"Logging file is set to {logging_file}, skipping logging.")
313+
else:
311314
if not os.path.isfile(logging_file):
312315
if logging_file == str(self.config_root_path / "logging.conf"):
313316
logger.warn(f"Default logging file in {logging_file} does not exist, skipping logging.")
314317
else:
315318
raise FileNotFoundError(f"Cannot find the logging config file: {logging_file}.")
316319
else:
317320
logger.info(f"Setting logging properties based on config: {logging_file}.")
318-
fileConfig(logging_file, disable_existing_loggers=False)
321+
fileConfig(str(logging_file), disable_existing_loggers=False)
319322

320323
self.parser = ConfigParser()
321324
self.parser.read_config(f=config_file)

monai/fl/client/monai_algo.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,14 @@ def initialize(self, extra=None):
134134
135135
Args:
136136
extra: Dict with additional information that should be provided by FL system,
137-
i.e., `ExtraItems.CLIENT_NAME` and `ExtraItems.APP_ROOT`.
137+
i.e., `ExtraItems.CLIENT_NAME`, `ExtraItems.APP_ROOT` and `ExtraItems.LOGGING_FILE`.
138+
You can diable the logging logic in the monai bundle by setting {ExtraItems.LOGGING_FILE} to False.
138139
139140
"""
140141
if extra is None:
141142
extra = {}
142143
self.client_name = extra.get(ExtraItems.CLIENT_NAME, "noname")
144+
logging_file = extra.get(ExtraItems.LOGGING_FILE, None)
143145
self.logger.info(f"Initializing {self.client_name} ...")
144146

145147
# FL platform needs to provide filepath to configuration files
@@ -149,7 +151,7 @@ def initialize(self, extra=None):
149151
if self.workflow is None:
150152
config_train_files = self._add_config_files(self.config_train_filename)
151153
self.workflow = ConfigWorkflow(
152-
config_file=config_train_files, meta_file=None, logging_file=None, workflow_type="train"
154+
config_file=config_train_files, meta_file=None, logging_file=logging_file, workflow_type="train"
153155
)
154156
self.workflow.initialize()
155157
self.workflow.bundle_root = self.bundle_root
@@ -412,13 +414,15 @@ def initialize(self, extra=None):
412414
413415
Args:
414416
extra: Dict with additional information that should be provided by FL system,
415-
i.e., `ExtraItems.CLIENT_NAME` and `ExtraItems.APP_ROOT`.
417+
i.e., `ExtraItems.CLIENT_NAME`, `ExtraItems.APP_ROOT` and `ExtraItems.LOGGING_FILE`.
418+
You can diable the logging logic in the monai bundle by setting {ExtraItems.LOGGING_FILE} to False.
416419
417420
"""
418421
self._set_cuda_device()
419422
if extra is None:
420423
extra = {}
421424
self.client_name = extra.get(ExtraItems.CLIENT_NAME, "noname")
425+
logging_file = extra.get(ExtraItems.LOGGING_FILE, None)
422426
timestamp = time.strftime("%Y%m%d_%H%M%S")
423427
self.logger.info(f"Initializing {self.client_name} ...")
424428
# FL platform needs to provide filepath to configuration files
@@ -434,7 +438,7 @@ def initialize(self, extra=None):
434438
self.train_workflow = ConfigWorkflow(
435439
config_file=config_train_files,
436440
meta_file=None,
437-
logging_file=None,
441+
logging_file=logging_file,
438442
workflow_type="train",
439443
**self.train_kwargs,
440444
)
@@ -459,7 +463,7 @@ def initialize(self, extra=None):
459463
self.eval_workflow = ConfigWorkflow(
460464
config_file=config_eval_files,
461465
meta_file=None,
462-
logging_file=None,
466+
logging_file=logging_file,
463467
workflow_type=self.eval_workflow_name,
464468
**self.eval_kwargs,
465469
)

monai/fl/utils/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ExtraItems(StrEnum):
3030
CLIENT_NAME = "fl_client_name"
3131
APP_ROOT = "fl_app_root"
3232
STATS_SENDER = "fl_stats_sender"
33+
LOGGING_FILE = "logging_file"
3334

3435

3536
class FlPhase(StrEnum):

0 commit comments

Comments
 (0)