diff --git a/docs/changelog/2356.feature.rst b/docs/changelog/2356.feature.rst new file mode 100644 index 000000000..dacd9423c --- /dev/null +++ b/docs/changelog/2356.feature.rst @@ -0,0 +1 @@ +Improve coloring of logged commands - by :user:`ssbarnea`. diff --git a/src/tox/report.py b/src/tox/report.py index c9b975330..ccff84af3 100644 --- a/src/tox/report.py +++ b/src/tox/report.py @@ -190,6 +190,8 @@ def format(self, record: logging.LogRecord) -> str: if record.levelno >= logging.ERROR: return self._error_formatter.format(record) if record.levelno >= logging.WARNING: + if self._is_colored and record.msg == "%s%s> %s" and record.args: + record.msg = f"%s{Style.NORMAL}%s{Style.DIM}>{Style.RESET_ALL} %s" return self._warning_formatter.format(record) return self._remaining_formatter.format(record) diff --git a/tests/test_report.py b/tests/test_report.py index 246276d1e..548262bd9 100644 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -20,7 +20,8 @@ def test_setup_report(mocker: MockerFixture, capsys: CaptureFixture, verbosity: try: logging.critical("critical") logging.error("error") - logging.warning("warning") + # special warning line that should be auto-colored + logging.warning("%s%s> %s", "warning", "foo", "bar") logging.info("info") logging.debug("debug") logging.log(logging.NOTSET, "not-set") # this should not be logged @@ -56,5 +57,10 @@ def test_setup_report(mocker: MockerFixture, capsys: CaptureFixture, verbosity: if color: assert f"{Style.RESET_ALL}" in out + # check that our Warning line using special format was colored + expected_warning_text = "W\x1b[0m\x1b[36m warning\x1b[22mfoo\x1b[2m>\x1b[0m bar\x1b[0m\x1b[2m" else: assert f"{Style.RESET_ALL}" not in out + expected_warning_text = "warningfoo> bar" + if verbosity >= 4: # where warnings are logged + assert expected_warning_text in lines[3]