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) diff --git a/src/mdacli/cli.py b/src/mdacli/cli.py index 92190d88..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 @@ -27,8 +26,6 @@ from .logger import setup_logging from .utils import _exit_if_a_is_b -logger = logging.getLogger(__name__) - def cli( name, @@ -113,11 +110,8 @@ 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(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..2057d1f7 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,9 +66,13 @@ def setup_logging( message logged from errors, warnings and infos will be displayed. """ try: - format = "" + # Get the root logger + logobj = logging.getLogger() + if level == logging.DEBUG: - format += "[{levelname}]:{filename}:{funcName}:{lineno} - " + format = "[{levelname}]:{filename}:{funcName}:{lineno} - " + else: + format = "" format += "{message}" formatter = logging.Formatter(format, style="{") @@ -90,8 +93,11 @@ 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()) @@ -99,8 +105,8 @@ def setup_logging( else: logobj.debug("Logging to file is disabled.") - for handler in handlers: - logobj.addHandler(handler) + # Redirect warnings (from the warnings library) to the logging system + logging.captureWarnings(True) yield 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")