Skip to content

Commit 5d94b23

Browse files
authored
Contain logger configuration (#318)
* only configure the iohub logger * add regression testing
1 parent 73c10c3 commit 5d94b23

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

iohub/__init__.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@
1414
__all__ = ["open_ome_zarr", "read_images"]
1515

1616

17-
_level = os.environ.get("IOHUB_LOG_LEVEL", logging.INFO)
18-
if str(_level).isdigit():
19-
_level = int(_level)
17+
def _configure_logging():
18+
"""Configure logging for iohub."""
19+
level = os.environ.get("IOHUB_LOG_LEVEL", logging.INFO)
20+
if str(level).isdigit():
21+
level = int(level)
22+
iohub_logger = logging.getLogger(__name__)
23+
iohub_logger.setLevel(level)
24+
stream_handler = logging.StreamHandler()
25+
stream_handler.setFormatter(
26+
logging.Formatter(fmt="%(levelname)s:%(name)s:%(message)s")
27+
)
28+
iohub_logger.addHandler(stream_handler)
2029

21-
logging.basicConfig()
22-
logging.getLogger(__name__).setLevel(_level)
30+
31+
_configure_logging()

tests/test_log_level.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import logging
2+
3+
import iohub # noqa: F401
4+
5+
6+
def test_external_logging_level(caplog):
7+
with caplog.at_level(logging.DEBUG):
8+
iohub_logger = logging.getLogger("iohub")
9+
assert iohub_logger.getEffectiveLevel() == logging.INFO
10+
other_logger = logging.getLogger(__name__)
11+
assert other_logger.getEffectiveLevel() == logging.DEBUG

0 commit comments

Comments
 (0)