From 6da2efbfc51e85791f824fb537a36d60dbf87b23 Mon Sep 17 00:00:00 2001 From: hejamu Date: Thu, 22 Jan 2026 11:17:06 +0100 Subject: [PATCH 1/5] Draft of new root logger handling --- src/mdacli/cli.py | 4 +--- src/mdacli/libcli.py | 8 -------- src/mdacli/logger.py | 13 ++++++++----- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/mdacli/cli.py b/src/mdacli/cli.py index 92190d88..f7a4decb 100644 --- a/src/mdacli/cli.py +++ b/src/mdacli/cli.py @@ -27,8 +27,6 @@ from .logger import setup_logging from .utils import _exit_if_a_is_b -logger = logging.getLogger(__name__) - def cli( name, @@ -117,7 +115,7 @@ def cli( # Ignore all warnings if not in debug mode, because MDA is noisy warnings.filterwarnings("ignore") - with setup_logging(logger, logfile=args.logfile, level=level): + with setup_logging(logfile=args.logfile, level=level): # Execute the main client interface. try: analysis_callable = args.analysis_callable diff --git a/src/mdacli/libcli.py b/src/mdacli/libcli.py index ecdada19..f1542bec 100644 --- a/src/mdacli/libcli.py +++ b/src/mdacli/libcli.py @@ -20,7 +20,6 @@ import MDAnalysis as mda from MDAnalysis.transformations.boxdimensions import set_dimensions -from .colors import Emphasise from .save import save from .utils import convert_str_time, parse_callable_signature, parse_docs @@ -45,13 +44,6 @@ } -def _warning(message, *args, **kwargs): # NOQA: ARG001 - logger.warning(Emphasise.warning(message)) - - -warnings.showwarning = _warning - - class KwargsDict(argparse.Action): """Convert input string to a dictionary. diff --git a/src/mdacli/logger.py b/src/mdacli/logger.py index e851b41c..13047750 100644 --- a/src/mdacli/logger.py +++ b/src/mdacli/logger.py @@ -48,7 +48,6 @@ def check_suffix(filename: str | Path, suffix: str) -> str | Path: @contextlib.contextmanager def setup_logging( - logobj: logging.Logger, logfile: str | Path | None = None, level: int = logging.WARNING, ): @@ -67,6 +66,8 @@ def setup_logging( message logged from errors, warnings and infos will be displayed. """ try: + logobj = logging.getLogger() + format = "" if level == logging.DEBUG: format += "[{levelname}]:{filename}:{funcName}:{lineno} - " @@ -90,18 +91,20 @@ def setup_logging( logging.addLevelName(logging.WARNING, Emphasise.warning("WARNING")) logging.addLevelName(logging.ERROR, Emphasise.error("ERROR")) - logging.basicConfig(format=format, handlers=handlers, level=level, style="{") logging.captureWarnings(True) + logobj.setLevel(level) + for handler in handlers: + handler.setLevel(level) + handler.setFormatter(formatter) + logobj.addHandler(handler) + if logfile: abs_path = str(Path(logfile).absolute().resolve()) logobj.info(f"This log is also available at '{abs_path}'.") else: logobj.debug("Logging to file is disabled.") - for handler in handlers: - logobj.addHandler(handler) - yield finally: From 7a932bcb4e05da52bdd2a4a69b20402914d2172a Mon Sep 17 00:00:00 2001 From: hejamu Date: Thu, 22 Jan 2026 11:23:22 +0100 Subject: [PATCH 2/5] Rewrite tests --- tests/test_logger.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/test_logger.py b/tests/test_logger.py index 21bf4912..862e3a31 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -41,9 +41,7 @@ def test_warnings_in_log(caplog): Keep this test at the top since it seems otherwise there are some pytest issues... """ - logger = logging.getLogger() - - with setup_logging(logger): + with setup_logging(): warnings.warn("A warning", stacklevel=1) assert "A warning" in caplog.text @@ -54,7 +52,7 @@ def test_default_log(caplog, capsys): caplog.set_level(logging.INFO) logger = logging.getLogger() - with setup_logging(logger, level=logging.INFO): + with setup_logging(level=logging.INFO): logger.info("foo") logger.debug("A debug message") @@ -72,7 +70,7 @@ def test_info_log(caplog, monkeypatch, tmp_path, capsys): caplog.set_level(logging.INFO) logger = logging.getLogger() - with setup_logging(logger, logfile="logfile.log", level=logging.INFO): + with setup_logging(logfile="logfile.log", level=logging.INFO): logger.info("foo") logger.debug("A debug message") @@ -97,7 +95,7 @@ def test_debug_log(caplog, monkeypatch, tmp_path, capsys): caplog.set_level(logging.DEBUG) logger = logging.getLogger() - with setup_logging(logger, logfile="logfile.log", level=logging.DEBUG): + with setup_logging(logfile="logfile.log", level=logging.DEBUG): logger.info("foo") logger.debug("A debug message") From 0f7f5cebf29cc61ca3244ec3a66fd747568e9430 Mon Sep 17 00:00:00 2001 From: Philip Loche Date: Thu, 22 Jan 2026 12:52:28 +0100 Subject: [PATCH 3/5] update changelog --- docs/CHANGELOG.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/CHANGELOG.rst b/docs/CHANGELOG.rst index b44c0143..9fe20cba 100644 --- a/docs/CHANGELOG.rst +++ b/docs/CHANGELOG.rst @@ -5,6 +5,7 @@ Changelog Unreleased ---------- +* Change internal handling of loggers * Fix installation link in README v0.1.34 (2025-12-02) From 039935941fa82e9a5f29c6ad2ff4d1ddc3d4b86a Mon Sep 17 00:00:00 2001 From: hejamu Date: Thu, 22 Jan 2026 18:49:59 +0100 Subject: [PATCH 4/5] Some smaller fixes - Clarify setup_logging - don't filter warnings --- src/mdacli/cli.py | 3 --- src/mdacli/logger.py | 11 +++++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/mdacli/cli.py b/src/mdacli/cli.py index f7a4decb..45b8dc9c 100644 --- a/src/mdacli/cli.py +++ b/src/mdacli/cli.py @@ -111,9 +111,6 @@ def cli( if args.debug: level = logging.DEBUG - else: - # Ignore all warnings if not in debug mode, because MDA is noisy - warnings.filterwarnings("ignore") with setup_logging(logfile=args.logfile, level=level): # Execute the main client interface. diff --git a/src/mdacli/logger.py b/src/mdacli/logger.py index 13047750..2057d1f7 100644 --- a/src/mdacli/logger.py +++ b/src/mdacli/logger.py @@ -66,11 +66,13 @@ def setup_logging( message logged from errors, warnings and infos will be displayed. """ try: + # Get the root logger logobj = logging.getLogger() - format = "" if level == logging.DEBUG: - format += "[{levelname}]:{filename}:{funcName}:{lineno} - " + format = "[{levelname}]:{filename}:{funcName}:{lineno} - " + else: + format = "" format += "{message}" formatter = logging.Formatter(format, style="{") @@ -91,8 +93,6 @@ def setup_logging( logging.addLevelName(logging.WARNING, Emphasise.warning("WARNING")) logging.addLevelName(logging.ERROR, Emphasise.error("ERROR")) - logging.captureWarnings(True) - logobj.setLevel(level) for handler in handlers: handler.setLevel(level) @@ -105,6 +105,9 @@ def setup_logging( else: logobj.debug("Logging to file is disabled.") + # Redirect warnings (from the warnings library) to the logging system + logging.captureWarnings(True) + yield finally: From b4a658bf29bf804210b44deba94fdcf683ff676b Mon Sep 17 00:00:00 2001 From: hejamu Date: Thu, 22 Jan 2026 18:51:50 +0100 Subject: [PATCH 5/5] Lint --- src/mdacli/cli.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/mdacli/cli.py b/src/mdacli/cli.py index 45b8dc9c..310e16b1 100644 --- a/src/mdacli/cli.py +++ b/src/mdacli/cli.py @@ -10,7 +10,6 @@ import logging import sys import traceback -import warnings import argcomplete from MDAnalysis.analysis.base import AnalysisBase