From ba5da68db6cd7b8e3af7b050adc466be58c0572a 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 --- src/pip/_internal/resolution/resolvelib/factory.py | 2 +- src/pip/_internal/utils/logging.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/pip/_internal/resolution/resolvelib/factory.py b/src/pip/_internal/resolution/resolvelib/factory.py index c3aaa6957f5..7d04625bdd5 100644 --- a/src/pip/_internal/resolution/resolvelib/factory.py +++ b/src/pip/_internal/resolution/resolvelib/factory.py @@ -692,7 +692,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 1c0cd8e261e..80a7b9b5d92 100644 --- a/src/pip/_internal/utils/logging.py +++ b/src/pip/_internal/utils/logging.py @@ -162,7 +162,10 @@ 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. + if hasattr(record, "color"): + style = Style(color=record.color) + elif record.levelno is not None: if record.levelno >= logging.ERROR: style = Style(color="red") elif record.levelno >= logging.WARNING: