Skip to content

Commit

Permalink
Merge pull request #3042 from abravalheri/issue-3038
Browse files Browse the repository at this point in the history
Ensure log level is set correctly (and `setuptools.logging.set_threshold` is called)
  • Loading branch information
abravalheri authored Jan 24, 2022
2 parents 120dd88 + 711b526 commit 1a1397a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions setuptools/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def configure():
format="{message}", style='{', handlers=handlers, level=logging.DEBUG)
monkey.patch_func(set_threshold, distutils.log, 'set_threshold')

# For some reason `distutils.log` module is getting cached in `distutils.dist`
# and then loaded again when patched,
# implying: id(distutils.log) != id(distutils.dist.log).
# Make sure the same module object is used everywhere:
distutils.dist.log = distutils.log


def set_threshold(level):
logging.root.setLevel(level*10)
Expand Down
36 changes: 36 additions & 0 deletions setuptools/tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import logging

import pytest


setup_py = """\
from setuptools import setup
setup(
name="test_logging",
version="0.0"
)
"""


@pytest.mark.parametrize(
"flag, expected_level", [("--dry-run", "INFO"), ("--verbose", "DEBUG")]
)
def test_verbosity_level(tmp_path, monkeypatch, flag, expected_level):
"""Make sure the correct verbosity level is set (issue #3038)"""
import setuptools # noqa: Import setuptools to monkeypatch distutils
import distutils # <- load distutils after all the patches take place

logger = logging.Logger(__name__)
monkeypatch.setattr(logging, "root", logger)
unset_log_level = logger.getEffectiveLevel()
assert logging.getLevelName(unset_log_level) == "NOTSET"

setup_script = tmp_path / "setup.py"
setup_script.write_text(setup_py)
dist = distutils.core.run_setup(setup_script, stop_after="init")
dist.script_args = [flag, "sdist"]
dist.parse_command_line() # <- where the log level is set
log_level = logger.getEffectiveLevel()
log_level_name = logging.getLevelName(log_level)
assert log_level_name == expected_level

0 comments on commit 1a1397a

Please sign in to comment.