Rework how the logging stack handles rich objects

This makes it possible to render content via rich without a magic string
and relies on a proper mechanism supported by the logging stack.
This commit is contained in:
Pradyun Gedam 2023-10-01 12:22:41 +01:00
parent 71df02c412
commit 3f6e81694f
No known key found for this signature in database
GPG Key ID: FF99710C4332258E
5 changed files with 7 additions and 7 deletions

View File

@ -181,7 +181,7 @@ class Command(CommandContextMixIn):
assert isinstance(status, int)
return status
except DiagnosticPipError as exc:
logger.error("[present-rich] %s", exc)
logger.error("%s", exc, extra={"rich": True})
logger.debug("Exception information:", exc_info=True)
return ERROR

View File

@ -233,7 +233,7 @@ def pip_self_version_check(session: PipSession, options: optparse.Values) -> Non
),
)
if upgrade_prompt is not None:
logger.warning("[present-rich] %s", upgrade_prompt)
logger.warning("%s", upgrade_prompt, extra={"rich": True})
except Exception:
logger.warning("There was an error checking the latest version of pip.")
logger.debug("See below for error", exc_info=True)

View File

@ -155,8 +155,8 @@ class RichPipStreamHandler(RichHandler):
# If we are given a diagnostic error to present, present it with indentation.
assert isinstance(record.args, tuple)
if record.msg == "[present-rich] %s" and len(record.args) == 1:
rich_renderable = record.args[0]
if getattr(record, "rich", False):
(rich_renderable,) = record.args
assert isinstance(
rich_renderable, (ConsoleRenderable, RichCast, str)
), f"{rich_renderable} is not rich-console-renderable"

View File

@ -209,7 +209,7 @@ def call_subprocess(
output_lines=all_output if not showing_subprocess else None,
)
if log_failed_cmd:
subprocess_logger.error("[present-rich] %s", error)
subprocess_logger.error("%s", error, extra={"rich": True})
subprocess_logger.verbose(
"[bold magenta]full command[/]: [blue]%s[/]",
escape(format_command_args(cmd)),

View File

@ -260,9 +260,9 @@ class TestCallSubprocess:
expected = (
None,
[
# pytest's caplog overrides th formatter, which means that we
# pytest's caplog overrides the formatter, which means that we
# won't see the message formatted through our formatters.
("pip.subprocessor", ERROR, "[present-rich]"),
("pip.subprocessor", ERROR, "subprocess error exited with 1"),
],
)
# The spinner should spin three times in this case since the