Skip to content

Commit c3bd0a4

Browse files
authored
Switch to root logger in setup_logging (#133)
1 parent ae99dba commit c3bd0a4

File tree

5 files changed

+19
-28
lines changed

5 files changed

+19
-28
lines changed

docs/CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Changelog
55
Unreleased
66
----------
77

8+
* Change internal handling of loggers
89
* Fix installation link in README
910

1011
v0.1.34 (2025-12-02)

src/mdacli/cli.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import logging
1111
import sys
1212
import traceback
13-
import warnings
1413

1514
import argcomplete
1615
from MDAnalysis.analysis.base import AnalysisBase
@@ -27,8 +26,6 @@
2726
from .logger import setup_logging
2827
from .utils import _exit_if_a_is_b
2928

30-
logger = logging.getLogger(__name__)
31-
3229

3330
def cli(
3431
name,
@@ -113,11 +110,8 @@ def cli(
113110

114111
if args.debug:
115112
level = logging.DEBUG
116-
else:
117-
# Ignore all warnings if not in debug mode, because MDA is noisy
118-
warnings.filterwarnings("ignore")
119113

120-
with setup_logging(logger, logfile=args.logfile, level=level):
114+
with setup_logging(logfile=args.logfile, level=level):
121115
# Execute the main client interface.
122116
try:
123117
analysis_callable = args.analysis_callable

src/mdacli/libcli.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import MDAnalysis as mda
2121
from MDAnalysis.transformations.boxdimensions import set_dimensions
2222

23-
from .colors import Emphasise
2423
from .save import save
2524
from .utils import convert_str_time, parse_callable_signature, parse_docs
2625

@@ -45,13 +44,6 @@
4544
}
4645

4746

48-
def _warning(message, *args, **kwargs): # NOQA: ARG001
49-
logger.warning(Emphasise.warning(message))
50-
51-
52-
warnings.showwarning = _warning
53-
54-
5547
class KwargsDict(argparse.Action):
5648
"""Convert input string to a dictionary.
5749

src/mdacli/logger.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def check_suffix(filename: str | Path, suffix: str) -> str | Path:
4848

4949
@contextlib.contextmanager
5050
def setup_logging(
51-
logobj: logging.Logger,
5251
logfile: str | Path | None = None,
5352
level: int = logging.WARNING,
5453
):
@@ -67,9 +66,13 @@ def setup_logging(
6766
message logged from errors, warnings and infos will be displayed.
6867
"""
6968
try:
70-
format = ""
69+
# Get the root logger
70+
logobj = logging.getLogger()
71+
7172
if level == logging.DEBUG:
72-
format += "[{levelname}]:{filename}:{funcName}:{lineno} - "
73+
format = "[{levelname}]:{filename}:{funcName}:{lineno} - "
74+
else:
75+
format = ""
7376
format += "{message}"
7477

7578
formatter = logging.Formatter(format, style="{")
@@ -90,17 +93,20 @@ def setup_logging(
9093
logging.addLevelName(logging.WARNING, Emphasise.warning("WARNING"))
9194
logging.addLevelName(logging.ERROR, Emphasise.error("ERROR"))
9295

93-
logging.basicConfig(format=format, handlers=handlers, level=level, style="{")
94-
logging.captureWarnings(True)
96+
logobj.setLevel(level)
97+
for handler in handlers:
98+
handler.setLevel(level)
99+
handler.setFormatter(formatter)
100+
logobj.addHandler(handler)
95101

96102
if logfile:
97103
abs_path = str(Path(logfile).absolute().resolve())
98104
logobj.info(f"This log is also available at '{abs_path}'.")
99105
else:
100106
logobj.debug("Logging to file is disabled.")
101107

102-
for handler in handlers:
103-
logobj.addHandler(handler)
108+
# Redirect warnings (from the warnings library) to the logging system
109+
logging.captureWarnings(True)
104110

105111
yield
106112

tests/test_logger.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ def test_warnings_in_log(caplog):
4141
Keep this test at the top since it seems otherwise there are some pytest
4242
issues...
4343
"""
44-
logger = logging.getLogger()
45-
46-
with setup_logging(logger):
44+
with setup_logging():
4745
warnings.warn("A warning", stacklevel=1)
4846

4947
assert "A warning" in caplog.text
@@ -54,7 +52,7 @@ def test_default_log(caplog, capsys):
5452
caplog.set_level(logging.INFO)
5553
logger = logging.getLogger()
5654

57-
with setup_logging(logger, level=logging.INFO):
55+
with setup_logging(level=logging.INFO):
5856
logger.info("foo")
5957
logger.debug("A debug message")
6058

@@ -72,7 +70,7 @@ def test_info_log(caplog, monkeypatch, tmp_path, capsys):
7270
caplog.set_level(logging.INFO)
7371
logger = logging.getLogger()
7472

75-
with setup_logging(logger, logfile="logfile.log", level=logging.INFO):
73+
with setup_logging(logfile="logfile.log", level=logging.INFO):
7674
logger.info("foo")
7775
logger.debug("A debug message")
7876

@@ -97,7 +95,7 @@ def test_debug_log(caplog, monkeypatch, tmp_path, capsys):
9795
caplog.set_level(logging.DEBUG)
9896
logger = logging.getLogger()
9997

100-
with setup_logging(logger, logfile="logfile.log", level=logging.DEBUG):
98+
with setup_logging(logfile="logfile.log", level=logging.DEBUG):
10199
logger.info("foo")
102100
logger.debug("A debug message")
103101

0 commit comments

Comments
 (0)