Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AMICI hiding all warnings #2243

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions python/sdist/amici/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ def _setup_logger(
level: Optional[int] = logging.WARNING,
console_output: Optional[bool] = True,
file_output: Optional[bool] = False,
capture_warnings: Optional[bool] = True,
capture_warnings: Optional[bool] = False,
) -> logging.Logger:
"""
Set up a new logging.Logger for AMICI logging
Set up a new :class:`logging.Logger` for AMICI logging.

:param level:
Logging level, typically using a constant like logging.INFO or
logging.DEBUG
Logging level, typically using a constant like :obj:`logging.INFO` or
:obj:`logging.DEBUG`

:param console_output:
Set up a default console log handler if True (default)
Set up a default console log handler if ``True`` (default)

:param file_output:
Supply a filename to copy all log output to that file, or
set to False to disable (default)
set to ``False`` to disable (default)

:param capture_warnings:
Capture warnings from Python's warnings module if True (default)
Capture warnings from Python's warnings module if ``True``

:return:
A :class:`logging.Logger` object for AMICI logging. Note that other
Expand Down Expand Up @@ -81,7 +81,12 @@ def _setup_logger(

log.setLevel(level)

py_warn_logger = logging.getLogger("py.warnings")

# Remove default logging handler
for handler in log.handlers:
if handler in py_warn_logger.handlers:
py_warn_logger.removeHandler(handler)
log.handlers = []

log_fmt = logging.Formatter(
Expand All @@ -105,7 +110,10 @@ def _setup_logger(
log.debug("Python version: %s", platform.python_version())
log.debug("Hostname: %s", socket.getfqdn())

logging.captureWarnings(capture_warnings)
if capture_warnings:
logging.captureWarnings(capture_warnings)
for handler in log.handlers:
py_warn_logger.addHandler(handler)

return log

Expand Down