From f3636d66669e3999120b7080d0f6ef347de8cc26 Mon Sep 17 00:00:00 2001 From: Ahmad Abedalqader Date: Wed, 5 Jan 2022 00:14:06 -0800 Subject: [PATCH] Decouple logs output color from the logging level Previously, each logging level was coupled with a specific output color. This isn't always ideal because we might need to log at a certain level but use a custom color (based on user feedback / see linked issue). This patch decouples the logs output color from the logging level. Fixes #10519 --- news/10519.bugfix.rst | 0 src/pip/_internal/resolution/resolvelib/factory.py | 2 +- src/pip/_internal/utils/logging.py | 6 +++++- tests/functional/test_install_reqs.py | 4 ++-- tests/functional/test_new_resolver_errors.py | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 news/10519.bugfix.rst diff --git a/news/10519.bugfix.rst b/news/10519.bugfix.rst new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/pip/_internal/resolution/resolvelib/factory.py b/src/pip/_internal/resolution/resolvelib/factory.py index a4c24b52a1b..75f5f4e4569 100644 --- a/src/pip/_internal/resolution/resolvelib/factory.py +++ b/src/pip/_internal/resolution/resolvelib/factory.py @@ -722,7 +722,7 @@ def describe_trigger(parent: Candidate) -> str: + "the dependency conflict\n" ) - logger.info(msg) + logger.critical(msg, extra={"color": "black"}) return DistributionNotFound( "ResolutionImpossible: for help visit " diff --git a/src/pip/_internal/utils/logging.py b/src/pip/_internal/utils/logging.py index c10e1f4ced6..99b9bdeaeb7 100644 --- a/src/pip/_internal/utils/logging.py +++ b/src/pip/_internal/utils/logging.py @@ -167,7 +167,11 @@ def emit(self, record: logging.LogRecord) -> None: else: message = self.format(record) renderable = self.render_message(record, message) - if record.levelno is not None: + # If a custom color is passed use it, otherwise use default colors. + color_attribute = "color" + if hasattr(record, color_attribute): + style = Style(color=getattr(record, color_attribute)) + elif record.levelno is not None: if record.levelno >= logging.ERROR: style = Style(color="red") elif record.levelno >= logging.WARNING: diff --git a/tests/functional/test_install_reqs.py b/tests/functional/test_install_reqs.py index 14e1056ae7a..49bf9aaf328 100644 --- a/tests/functional/test_install_reqs.py +++ b/tests/functional/test_install_reqs.py @@ -696,8 +696,8 @@ def test_install_distribution_union_with_versions( ) if resolver_variant == "2020-resolver": assert "Cannot install localextras[bar]" in result.stderr - assert ("localextras[bar] 0.0.1 depends on localextras 0.0.1") in result.stdout - assert ("localextras[baz] 0.0.2 depends on localextras 0.0.2") in result.stdout + assert ("localextras[bar] 0.0.1 depends on localextras 0.0.1") in result.stderr + assert ("localextras[baz] 0.0.2 depends on localextras 0.0.2") in result.stderr else: assert ( "Successfully installed LocalExtras-0.0.1 simple-3.0 singlemodule-0.0.1" diff --git a/tests/functional/test_new_resolver_errors.py b/tests/functional/test_new_resolver_errors.py index 62304131283..6be3e4ce957 100644 --- a/tests/functional/test_new_resolver_errors.py +++ b/tests/functional/test_new_resolver_errors.py @@ -67,7 +67,7 @@ def test_new_resolver_conflict_constraints_file( assert "ResolutionImpossible" in result.stderr, str(result) message = "The user requested (constraint) pkg!=1.0" - assert message in result.stdout, str(result) + assert message in result.stderr, str(result) def test_new_resolver_requires_python_error(script: PipTestEnvironment) -> None: