Skip to content

Commit 6372af5

Browse files
JoeZiminskiadamltysonpre-commit-ci[bot]
authored
Handle multiprocessing for named loggers. (#34)
* Use created logger for multiprocessing setup. * Instead raise if trying to multiprocess log not using root. * Add test. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Adam Tyson <code@adamltyson.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a860778 commit 6372af5

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

fancylog/fancylog.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,21 @@ def setup_logging(
292292
:param logger_name: If None, logger uses default logger. Otherwise,
293293
logger name is set to `logger_name`.
294294
"""
295-
initialise_logger(
295+
logger = initialise_logger(
296296
filename,
297297
print_level=print_level,
298298
file_level=file_level,
299299
log_to_console=log_to_console,
300300
logger_name=logger_name,
301301
)
302302
if multiprocessing_aware:
303+
if logger_name:
304+
raise ValueError(
305+
"`multiprocessing_aware` is not supported"
306+
"with `logger_name`. Multiprocess logging"
307+
"must be performed with the root logger."
308+
)
309+
303310
try:
304311
import multiprocessing_logging
305312

@@ -316,8 +323,8 @@ def setup_logging(
316323
"multiple processes."
317324
)
318325
else:
319-
logging.info("Starting logging")
320-
logging.info("Not logging multiple processes")
326+
logger.info("Starting logging")
327+
logger.info("Not logging multiple processes")
321328

322329

323330
def disable_logging():

tests/tests/test_general.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22

3+
import pytest
34
from rich.logging import RichHandler
45

56
import fancylog
@@ -42,6 +43,22 @@ def test_logger_name(tmp_path):
4243
assert logger_name in logging.root.manager.loggerDict
4344

4445

46+
def test_assert_named_logger_with_multiprocessing(tmp_path):
47+
"""
48+
Test an error is raised if trying to use multiprocess
49+
logging with a named logger.
50+
"""
51+
with pytest.raises(ValueError) as e:
52+
fancylog.start_logging(
53+
tmp_path,
54+
fancylog,
55+
logger_name="hello_world",
56+
multiprocessing_aware=True,
57+
)
58+
59+
assert "root logger" in str(e.value)
60+
61+
4562
def test_logging_to_console(tmp_path, capsys):
4663
"""
4764
Check that logs are written to stdout when

0 commit comments

Comments
 (0)