Skip to content

Commit f7f5d68

Browse files
authored
Improve coloring of logged commands (#2546)
Fixes: #2356
1 parent b39df80 commit f7f5d68

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

docs/changelog/2356.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve coloring of logged commands - by :user:`ssbarnea`.

src/tox/report.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ def format(self, record: logging.LogRecord) -> str:
190190
if record.levelno >= logging.ERROR:
191191
return self._error_formatter.format(record)
192192
if record.levelno >= logging.WARNING:
193+
if self._is_colored and record.msg == "%s%s> %s" and record.args:
194+
record.msg = f"%s{Style.NORMAL}%s{Style.DIM}>{Style.RESET_ALL} %s"
193195
return self._warning_formatter.format(record)
194196
return self._remaining_formatter.format(record)
195197

tests/test_report.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def test_setup_report(mocker: MockerFixture, capsys: CaptureFixture, verbosity:
2020
try:
2121
logging.critical("critical")
2222
logging.error("error")
23-
logging.warning("warning")
23+
# special warning line that should be auto-colored
24+
logging.warning("%s%s> %s", "warning", "foo", "bar")
2425
logging.info("info")
2526
logging.debug("debug")
2627
logging.log(logging.NOTSET, "not-set") # this should not be logged
@@ -56,5 +57,10 @@ def test_setup_report(mocker: MockerFixture, capsys: CaptureFixture, verbosity:
5657

5758
if color:
5859
assert f"{Style.RESET_ALL}" in out
60+
# check that our Warning line using special format was colored
61+
expected_warning_text = "W\x1b[0m\x1b[36m warning\x1b[22mfoo\x1b[2m>\x1b[0m bar\x1b[0m\x1b[2m"
5962
else:
6063
assert f"{Style.RESET_ALL}" not in out
64+
expected_warning_text = "warningfoo> bar"
65+
if verbosity >= 4: # where warnings are logged
66+
assert expected_warning_text in lines[3]

0 commit comments

Comments
 (0)