Skip to content

Commit 5c27e79

Browse files
committed
add a warning: setup_logging() should only be called once.
1 parent 17b2f4b commit 5c27e79

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,9 @@ there are loggers in submodules, these loggers do not inspect exception when you
656656
That is because there class was different than loggers created after importing `logger_tt`.
657657
Now all loggers have the same new class regardless the point of importing `logger_tt`.
658658

659+
**setup_logging()**: This function should only be called once.
660+
Add a warning if it is called more than one time.
661+
659662
## 1.5.1
660663
* Use `socketHandler` as default for multiprocessing.
661664
Under linux, to use `queueHandler`, user must pass `use_multiprocessing="fork"` to `setup_logging`

logger_tt/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ def setup_logging(config_path="", log_path="", **logger_tt_config) -> LogConfig:
111111
This flag switches the queue used for logging from
112112
queue.Queue to multiprocessing.Queue . This option can only be used here.
113113
"""
114+
if internal_config.initialized:
115+
logger.warning('Re-initializing logger_tt. "setup_logging()" should only be called one.')
116+
114117
if config_path:
115118
cfgpath = Path(config_path)
116119
assert cfgpath.is_file(), 'Input config path is not a file!'

logger_tt/core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ def __init__(self):
4343
# suppress logger list for usage in filter
4444
self.suppress_list = set()
4545

46+
# initialized counter
47+
self.__initialized = 0
48+
49+
@property
50+
def initialized(self):
51+
return self.__initialized
52+
4653
def from_dict(self, odict: dict):
4754
# store basic settings
4855
for key in ['full_context', 'strict', 'guess_level']:
@@ -63,6 +70,8 @@ def from_dict(self, odict: dict):
6370
# set logging mode accordingly
6471
self.set_mode(odict['use_multiprocessing'])
6572

73+
self.__initialized += 1
74+
6675
def set_mode(self, use_multiprocessing):
6776
"""Select logging method according to platform and multiprocessing"""
6877
os_name = platform.system()

tests/test_simple.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,12 @@ def test_suppress_logger(capsys, level):
109109

110110
def test_suppress_logger2(capsys):
111111
# suppress by the code
112-
with setup_logging(suppress=['urllib3']):
113-
from tests.exchangelib_logger import fox_run
114-
115-
fox_run()
112+
with setup_logging(suppress=['suppressed_logger']):
113+
logger = getLogger('suppressed_logger')
114+
logger.info('Ha ha, this should not be logged')
116115

117116
stdout_data = capsys.readouterr().out
118-
assert 'DEBUG' not in stdout_data
119-
assert 'INFO' in stdout_data
120-
assert 'WARNING' in stdout_data
121-
assert 'ERROR' in stdout_data
122-
assert 'CRITICAL' in stdout_data
117+
assert 'INFO' not in stdout_data
123118

124119

125120
def test_logging_disabled(capsys):
@@ -190,3 +185,13 @@ def test_default_logger_suppress():
190185

191186
log_data = log.read_text()
192187
assert len(re.findall(r"tests.sub_module:\d+ INFO", log_data)) == 0
188+
189+
190+
def test_double_setup_logging():
191+
# there should be a warning if setup_logging() is called multiple times
192+
with setup_logging() as log_config:
193+
with setup_logging() as log_config:
194+
pass
195+
196+
log_data = log.read_text()
197+
assert "WARNING" in log_data

0 commit comments

Comments
 (0)