Skip to content

Commit b179546

Browse files
authored
Unset loglevel in amici.sim.sundials._swig_wrappers (#3150)
This way, that logger will inherit the log level from its parents, and users can set the log level with, e.g., `logging.getLogger("amici.sim.sundials").setLevel(logging.CRITICAL)` without exposing any private modules. Related to #3081. `
1 parent f6f610f commit b179546

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

python/sdist/amici/logging.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ def _setup_logger(
118118
return log
119119

120120

121-
def set_log_level(logger: logging.Logger, log_level: int | bool) -> None:
121+
def set_log_level(
122+
logger: logging.Logger, log_level: int | bool | None
123+
) -> None:
122124
if log_level is not None and log_level is not False:
123125
if isinstance(log_level, bool):
124126
log_level = logging.DEBUG
@@ -139,33 +141,33 @@ def get_logger(
139141
**kwargs,
140142
) -> logging.Logger:
141143
"""
142-
Returns (if extistant) or creates an AMICI logger
144+
Returns (if exists) or creates an AMICI logger
143145
144146
If the AMICI base logger has already been set up, this method will
145147
return it or any of its descendant loggers without overriding the
146148
settings - i.e. any values supplied as kwargs will be ignored.
147149
148150
:param logger_name:
149-
Get a logger for a specific namespace, typically __name__
150-
for code outside of classes or self.__module__ inside a class
151+
Get a logger for a specific namespace, typically ``__name__``
152+
for code outside of classes or ``self.__module__`` inside a class
151153
152154
:param log_level:
153155
Override the default or preset log level for the requested logger.
154-
None or False uses the default or preset value. True evaluates to
155-
logging.DEBUG. Any integer is used directly.
156+
``None`` or ``False`` uses the default or preset value. ``True`` evaluates to
157+
``logging.DEBUG``. Any integer is used directly.
156158
157159
:param console_output:
158-
Set up a default console log handler if True (default). Only used when
160+
Set up a default console log handler if ``True`` (default). Only used when
159161
the AMICI logger hasn't been set up yet.
160162
161163
:param file_output:
162164
Supply a filename to copy all log output to that file, or set to
163-
False to disable (default). Only used when the AMICI logger hasn't
165+
``False`` to disable (default). Only used when the AMICI logger hasn't
164166
been set up yet.
165167
166168
:param capture_warnings:
167169
Capture warnings from Python's warnings module if True (default).
168-
Only used when the AMICI logger hasn't been set up yet..
170+
Only used when the AMICI logger hasn't been set up yet.
169171
170172
:return:
171173
A logging.Logger object with the requested name

python/sdist/amici/sim/sundials/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
for analyzing the simulation results.
77
"""
88

9+
import logging
910
import os
1011
import warnings
1112
from types import ModuleType
1213
from typing import Protocol, runtime_checkable
1314

1415
import amici
1516
from amici import amici_path, import_model_module
17+
from amici.logging import get_logger
18+
19+
logger = get_logger(__name__, log_level=logging.DEBUG)
20+
1621

1722
#: boolean indicating if this is the full package with swig interface or
1823
# the raw package without extension

python/sdist/amici/sim/sundials/_swig_wrappers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
from . import ReturnDataView
3232

33-
logger = get_logger(__name__, log_level=logging.DEBUG)
33+
logger = get_logger(__name__, log_level=None)
3434

3535

3636
__all__ = [

python/tests/test_sbml_import.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests related to amici.importers.sbml"""
22

3+
import logging
34
import os
45
import re
56
import sys
@@ -229,8 +230,23 @@ def test_logging_works(observable_dependent_error_model, caplog):
229230

230231
rdata = run_simulation(model, solver)
231232
assert rdata.status != AMICI_SUCCESS
233+
# this is a DEBUG level message and should be there by default
232234
assert "mxstep steps taken" in caplog.text
233235

236+
caplog.clear()
237+
238+
# ensure that we can control amici.sim.sundials._swig_wrappers logging
239+
# via the public amici.sim.sundials logger
240+
logger = logging.getLogger("amici.sim.sundials")
241+
old_lvl = logger.level
242+
try:
243+
logger.setLevel(logging.WARNING)
244+
rdata = run_simulation(model, solver)
245+
assert rdata.status != AMICI_SUCCESS
246+
assert "mxstep steps taken" not in caplog.text
247+
finally:
248+
logger.setLevel(old_lvl)
249+
234250

235251
@skip_on_valgrind
236252
def test_model_module_is_set(observable_dependent_error_model):

0 commit comments

Comments
 (0)