Skip to content

Commit

Permalink
Decouple logs output color from the logging level
Browse files Browse the repository at this point in the history
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
  • Loading branch information
healthy-pod committed May 8, 2022
1 parent a4f7508 commit d669bdc
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
Empty file added news/10519.bugfix.rst
Empty file.
2 changes: 1 addition & 1 deletion src/pip/_internal/resolution/resolvelib/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,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 "
Expand Down
6 changes: 5 additions & 1 deletion src/pip/_internal/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,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:
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/test_install_reqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,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"
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_new_resolver_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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:
Expand Down

0 comments on commit d669bdc

Please sign in to comment.