Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
Unreleased
----------

* Change internal handling of loggers
* Fix installation link in README

v0.1.34 (2025-12-02)
Expand Down
8 changes: 1 addition & 7 deletions src/mdacli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import logging
import sys
import traceback
import warnings

import argcomplete
from MDAnalysis.analysis.base import AnalysisBase
Expand All @@ -27,8 +26,6 @@
from .logger import setup_logging
from .utils import _exit_if_a_is_b

logger = logging.getLogger(__name__)


def cli(
name,
Expand Down Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions src/mdacli/libcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.

Expand Down
20 changes: 13 additions & 7 deletions src/mdacli/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
):
Expand All @@ -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="{")
Expand All @@ -90,17 +93,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)
# Redirect warnings (from the warnings library) to the logging system
logging.captureWarnings(True)

yield

Expand Down
10 changes: 4 additions & 6 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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")

Expand Down