Repository: Project-MONAI/MONAI

Issue:
Monai import modifies logger
**Describe the bug**
Importing `monai` modifies the python root logger leaving it with a `StreamHandler` for stdout. This can lead to double messages when a user instantiates their own logger.

**To Reproduce**
Run the following script
```python
import logging
import monai

logger = logging.getLogger(__name__)
stderr = logging.StreamHandler()
logger.addHandler(stderr)

logger.warning('hello, world')
```
Running this prints the 'hello, world' warning twice. The second time with date and time info:
<img width="368" alt="image" src="https://user-images.githubusercontent.com/7997179/217647170-89ec39cb-363d-430d-a052-34e9ef362d7e.png">

**Expected behavior**
It should just print `hello, world`. It does this if you comment out the `import monai` statement.

**Environment**

Printing MONAI config...
MONAI version: 1.1.0
Numpy version: 1.24.1
Pytorch version: 1.13.0+cu117
MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False
MONAI rev id: a2ec3752f54bfc3b40e7952234fbeb5452ed63e3
MONAI __file__: /usr/local/lib/python3.9/site-packages/monai/__init__.py

Optional dependencies:
Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.
Nibabel version: 5.0.0
scikit-image version: 0.19.3
Pillow version: 9.4.0
Tensorboard version: NOT INSTALLED or UNKNOWN VERSION.
gdown version: NOT INSTALLED or UNKNOWN VERSION.
TorchVision version: NOT INSTALLED or UNKNOWN VERSION.
tqdm version: NOT INSTALLED or UNKNOWN VERSION.
lmdb version: NOT INSTALLED or UNKNOWN VERSION.
psutil version: 5.9.4
pandas version: NOT INSTALLED or UNKNOWN VERSION.
einops version: NOT INSTALLED or UNKNOWN VERSION.
transformers version: NOT INSTALLED or UNKNOWN VERSION.
mlflow version: NOT INSTALLED or UNKNOWN VERSION.
pynrrd version: NOT INSTALLED or UNKNOWN VERSION.

For details about installing the optional dependencies, please visit:
    https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies

Printing system config...
System: Linux
Linux version: Debian GNU/Linux 11 (bullseye)
Platform: Linux-5.4.0-122-generic-x86_64-with-glibc2.31
Processor:
Machine: x86_64
Python version: 3.9.13
Process name: python
Command: ['python', 'monai_logger.py']
Open files: []
Num physical CPUs: 64
Num logical CPUs: 64
Num usable CPUs: 64
CPU usage (%): [8.5, 6.9, 100.0, 6.0, 10.1, 6.8, 8.5, 17.9, 6.8, 7.7, 6.8, 7.6, 7.7, 6.8, 25.6, 6.8, 6.1, 9.3, 7.8, 7.8, 8.5, 9.4, 9.4, 10.2, 8.5, 9.3, 7.6, 8.5, 9.2, 7.6, 9.4, 6.8, 8.5, 0.0, 5.1, 4.3, 0.9, 0.0, 7.0, 5.1, 0.0, 0.8, 0.0, 3.4, 0.0, 0.0, 5.1, 0.0, 0.9, 0.0, 0.0, 0.0, 1.7, 5.1, 5.1, 20.7, 0.0, 13.7, 0.9, 0.8, 7.0, 0.0, 9.5, 1.7]
CPU freq. (MHz): 3392
Load avg. in last 1, 5, 15 mins (%): [4.3, 2.9, 2.7]
Disk usage (%): 14.2
Avg. sensor temp. (Celsius): UNKNOWN for given OS
Total physical memory (GB): 246.0
Available memory (GB): 229.2
Used memory (GB): 15.0

Printing GPU config...
Num GPUs: 0
Has CUDA: False
cuDNN enabled: True
cuDNN version: 8500
None

Monai import modifies logger
**Describe the bug**
Importing `monai` modifies the python root logger leaving it with a `StreamHandler` for stdout. This can lead to double messages when a user instantiates their own logger.

**To Reproduce**
Run the following script
```python
import logging
import monai

logger = logging.getLogger(__name__)
stderr = logging.StreamHandler()
logger.addHandler(stderr)

logger.warning('hello, world')
```
Running this prints the 'hello, world' warning twice. The second time with date and time info:
<img width="368" alt="image" src="https://user-images.githubusercontent.com/7997179/217647170-89ec39cb-363d-430d-a052-34e9ef362d7e.png">

**Expected behavior**
It should just print `hello, world`. It does this if you comment out the `import monai` statement.

**Environment**

Printing MONAI config...
MONAI version: 1.1.0
Numpy version: 1.24.1
Pytorch version: 1.13.0+cu117
MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False
MONAI rev id: a2ec3752f54bfc3b40e7952234fbeb5452ed63e3
MONAI __file__: /usr/local/lib/python3.9/site-packages/monai/__init__.py

Optional dependencies:
Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.
Nibabel version: 5.0.0
scikit-image version: 0.19.3
Pillow version: 9.4.0
Tensorboard version: NOT INSTALLED or UNKNOWN VERSION.
gdown version: NOT INSTALLED or UNKNOWN VERSION.
TorchVision version: NOT INSTALLED or UNKNOWN VERSION.
tqdm version: NOT INSTALLED or UNKNOWN VERSION.
lmdb version: NOT INSTALLED or UNKNOWN VERSION.
psutil version: 5.9.4
pandas version: NOT INSTALLED or UNKNOWN VERSION.
einops version: NOT INSTALLED or UNKNOWN VERSION.
transformers version: NOT INSTALLED or UNKNOWN VERSION.
mlflow version: NOT INSTALLED or UNKNOWN VERSION.
pynrrd version: NOT INSTALLED or UNKNOWN VERSION.

For details about installing the optional dependencies, please visit:
    https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies

Printing system config...
System: Linux
Linux version: Debian GNU/Linux 11 (bullseye)
Platform: Linux-5.4.0-122-generic-x86_64-with-glibc2.31
Processor:
Machine: x86_64
Python version: 3.9.13
Process name: python
Command: ['python', 'monai_logger.py']
Open files: []
Num physical CPUs: 64
Num logical CPUs: 64
Num usable CPUs: 64
CPU usage (%): [8.5, 6.9, 100.0, 6.0, 10.1, 6.8, 8.5, 17.9, 6.8, 7.7, 6.8, 7.6, 7.7, 6.8, 25.6, 6.8, 6.1, 9.3, 7.8, 7.8, 8.5, 9.4, 9.4, 10.2, 8.5, 9.3, 7.6, 8.5, 9.2, 7.6, 9.4, 6.8, 8.5, 0.0, 5.1, 4.3, 0.9, 0.0, 7.0, 5.1, 0.0, 0.8, 0.0, 3.4, 0.0, 0.0, 5.1, 0.0, 0.9, 0.0, 0.0, 0.0, 1.7, 5.1, 5.1, 20.7, 0.0, 13.7, 0.9, 0.8, 7.0, 0.0, 9.5, 1.7]
CPU freq. (MHz): 3392
Load avg. in last 1, 5, 15 mins (%): [4.3, 2.9, 2.7]
Disk usage (%): 14.2
Avg. sensor temp. (Celsius): UNKNOWN for given OS
Total physical memory (GB): 246.0
Available memory (GB): 229.2
Used memory (GB): 15.0

Printing GPU config...
Num GPUs: 0
Has CUDA: False
cuDNN enabled: True
cuDNN version: 8500
None



Discussion:

Resolving patch:
diff --git a/monai/fl/client/monai_algo.py b/monai/fl/client/monai_algo.py
--- a/monai/fl/client/monai_algo.py
+++ b/monai/fl/client/monai_algo.py
@@ -13,7 +13,6 @@
 
 import logging
 import os
-import sys
 from collections.abc import Mapping, MutableMapping
 from typing import Any, cast
 
@@ -22,6 +21,7 @@
 
 import monai
 from monai.apps.auto3dseg.data_analyzer import DataAnalyzer
+from monai.apps.utils import get_logger
 from monai.auto3dseg import SegSummarizer
 from monai.bundle import DEFAULT_EXP_MGMT_SETTINGS, ConfigComponent, ConfigItem, ConfigParser, patch_bundle_tracking
 from monai.engines import SupervisedTrainer, Trainer
@@ -41,7 +41,7 @@
 from monai.utils import min_version, require_pkg
 from monai.utils.enums import DataStatsKeys
 
-logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(asctime)s - %(message)s")
+logger = get_logger(__name__)
 
 
 def convert_global_weights(global_weights: Mapping, local_var_dict: MutableMapping) -> tuple[MutableMapping, int]:
@@ -116,7 +116,7 @@ def __init__(
         data_stats_transform_list: list | None = None,
         histogram_only: bool = False,
     ):
-        self.logger = logging.getLogger(self.__class__.__name__)
+        self.logger = logger
         self.bundle_root = bundle_root
         self.config_train_filename = config_train_filename
         self.config_filters_filename = config_filters_filename
@@ -405,7 +405,7 @@ def __init__(
         data_stats_transform_list: list | None = None,
         tracking: str | dict | None = None,
     ):
-        self.logger = logging.getLogger(self.__class__.__name__)
+        self.logger = logger
         if config_evaluate_filename == "default":
             # by default, evaluator needs both training and evaluate to be instantiated.
             config_evaluate_filename = ["configs/train.json", "configs/evaluate.json"]


Test patch: