Skip to content

Commit d669bdc

Browse files
committed
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
1 parent a4f7508 commit d669bdc

File tree

5 files changed

+9
-5
lines changed

5 files changed

+9
-5
lines changed

news/10519.bugfix.rst

Whitespace-only changes.

src/pip/_internal/resolution/resolvelib/factory.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ def describe_trigger(parent: Candidate) -> str:
730730
+ "the dependency conflict\n"
731731
)
732732

733-
logger.info(msg)
733+
logger.critical(msg, extra={"color": "black"})
734734

735735
return DistributionNotFound(
736736
"ResolutionImpossible: for help visit "

src/pip/_internal/utils/logging.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@ def emit(self, record: logging.LogRecord) -> None:
162162
else:
163163
message = self.format(record)
164164
renderable = self.render_message(record, message)
165-
if record.levelno is not None:
165+
# If a custom color is passed use it, otherwise use default colors.
166+
color_attribute = "color"
167+
if hasattr(record, color_attribute):
168+
style = Style(color=getattr(record, color_attribute))
169+
elif record.levelno is not None:
166170
if record.levelno >= logging.ERROR:
167171
style = Style(color="red")
168172
elif record.levelno >= logging.WARNING:

tests/functional/test_install_reqs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,8 @@ def test_install_distribution_union_with_versions(
695695
)
696696
if resolver_variant == "2020-resolver":
697697
assert "Cannot install localextras[bar]" in result.stderr
698-
assert ("localextras[bar] 0.0.1 depends on localextras 0.0.1") in result.stdout
699-
assert ("localextras[baz] 0.0.2 depends on localextras 0.0.2") in result.stdout
698+
assert ("localextras[bar] 0.0.1 depends on localextras 0.0.1") in result.stderr
699+
assert ("localextras[baz] 0.0.2 depends on localextras 0.0.2") in result.stderr
700700
else:
701701
assert (
702702
"Successfully installed LocalExtras-0.0.1 simple-3.0 singlemodule-0.0.1"

tests/functional/test_new_resolver_errors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_new_resolver_conflict_constraints_file(
6868
assert "ResolutionImpossible" in result.stderr, str(result)
6969

7070
message = "The user requested (constraint) pkg!=1.0"
71-
assert message in result.stdout, str(result)
71+
assert message in result.stderr, str(result)
7272

7373

7474
def test_new_resolver_requires_python_error(script: PipTestEnvironment) -> None:

0 commit comments

Comments
 (0)