From e353f77767f13d8990325ed0549bbf7d57cb3479 Mon Sep 17 00:00:00 2001 From: Luca Foppiano Date: Mon, 9 Nov 2026 07:51:59 +0100 Subject: [PATCH 01/14] Converters: do not configure the root logger at import time Both converter modules called logging.basicConfig(level=INFO) when imported, and grobid_client imports TEI2LossyJSON, so any program using the library got a root handler at INFO. It also made the format CLIs' setup_logging() a no-op: basicConfig does nothing once the root logger has a handler, so --verbose and the CLI log format were ignored. TEI2LossyJSON's logger did not propagate either, so an application's handlers never received its warnings. --- grobid_client/format/TEI2LossyJSON.py | 10 ++---- grobid_client/format/TEI2Markdown.py | 7 ++-- tests/test_library_logging.py | 49 +++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 tests/test_library_logging.py diff --git a/grobid_client/format/TEI2LossyJSON.py b/grobid_client/format/TEI2LossyJSON.py index c7e2a9c..5853111 100644 --- a/grobid_client/format/TEI2LossyJSON.py +++ b/grobid_client/format/TEI2LossyJSON.py @@ -19,14 +19,10 @@ import dateparser from bs4 import BeautifulSoup, Tag -# Configure module-level logger +# Module-level logger. Handlers and levels are the application's business: +# configuring the root logger at import time would override the logging +# set up by whoever imports this module (the format CLIs included). logger: logging.Logger = logging.getLogger(__name__) -logger.propagate = False # Prevent propagation to avoid duplicate logs - -# Only configure basic logging if nothing is set up yet -if not logger.handlers and not logging.getLogger().handlers: - # Basic configuration if not already configured by the application - logging.basicConfig(level=logging.INFO) class TEI2LossyJSONConverter: diff --git a/grobid_client/format/TEI2Markdown.py b/grobid_client/format/TEI2Markdown.py index 946453e..2a4a69b 100644 --- a/grobid_client/format/TEI2Markdown.py +++ b/grobid_client/format/TEI2Markdown.py @@ -20,11 +20,10 @@ import logging from datetime import datetime -# Configure module-level logger +# Module-level logger. Handlers and levels are the application's business: +# configuring the root logger at import time would override the logging +# set up by whoever imports this module (the format CLIs included). logger = logging.getLogger(__name__) -if not logger.handlers: - # Basic configuration if not already configured by the application - logging.basicConfig(level=logging.INFO) class TEI2MarkdownConverter: diff --git a/tests/test_library_logging.py b/tests/test_library_logging.py new file mode 100644 index 0000000..2d2fa1f --- /dev/null +++ b/tests/test_library_logging.py @@ -0,0 +1,49 @@ +""" +Importing the package must leave the application's logging alone. + +Both converter modules called logging.basicConfig(level=INFO) at import +time. Importing grobid_client (which imports TEI2LossyJSON) installed a +root handler at INFO in any program using the library, and turned the +format CLIs' own setup_logging() into a no-op, since basicConfig does +nothing once the root logger has a handler. TEI2LossyJSON's logger also +did not propagate, so an application's handlers never saw its warnings. +""" +import io +import logging +import subprocess +import sys + +import pytest + +from grobid_client.format.TEI2LossyJSON import TEI2LossyJSONConverter +from grobid_client.format.TEI2Markdown import TEI2MarkdownConverter + + +@pytest.mark.parametrize("module", [ + "grobid_client.grobid_client", + "grobid_client.format.TEI2LossyJSON", + "grobid_client.format.TEI2Markdown", +]) +def test_import_does_not_configure_the_root_logger(module): + code = (f"import logging, {module}; r = logging.getLogger(); " + "print(len(r.handlers), logging.getLevelName(r.level))") + out = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True, check=True) + assert out.stdout.split() == ["0", "WARNING"] + + +@pytest.mark.parametrize("converter", [TEI2LossyJSONConverter, TEI2MarkdownConverter]) +def test_converter_warnings_reach_the_application_handlers(converter, caplog): + with caplog.at_level(logging.WARNING): + assert converter().convert_tei_file(io.StringIO("")) is None + assert any("not well-formed" in r.getMessage() for r in caplog.records) + + +@pytest.mark.parametrize("name", ["TEI2LossyJSON", "TEI2Markdown"]) +def test_format_cli_applies_its_own_log_format(name, tmp_path): + empty = tmp_path / "empty.tei.xml" + empty.write_text("") + out = subprocess.run([sys.executable, "-m", "grobid_client.format", name, "--input", str(empty)], + capture_output=True, text=True) + assert out.returncode == 1 + # setup_logging()'s format: "