Skip to content

Commit 3ae0103

Browse files
authored
Merge pull request #8641 from rahul-kumi/fix/8548
add support for precision bit in LEVEL_NAME_FMT regex
2 parents 886fd70 + 9e11d64 commit 3ae0103

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

changelog/8548.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Introduce fix to handle precision width in ``log-cli-format`` in turn to fix output coloring for certain formats.

src/_pytest/logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ColoredLevelFormatter(logging.Formatter):
5959
logging.DEBUG: {"purple"},
6060
logging.NOTSET: set(),
6161
}
62-
LEVELNAME_FMT_REGEX = re.compile(r"%\(levelname\)([+-.]?\d*s)")
62+
LEVELNAME_FMT_REGEX = re.compile(r"%\(levelname\)([+-.]?\d*(?:\.\d+)?s)")
6363

6464
def __init__(self, terminalwriter: TerminalWriter, *args, **kwargs) -> None:
6565
super().__init__(*args, **kwargs)

testing/logging/test_formatter.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,32 @@ def test_coloredlogformatter() -> None:
1818
exc_info=None,
1919
)
2020

21-
class ColorConfig:
22-
class option:
23-
pass
21+
tw = TerminalWriter()
22+
tw.hasmarkup = True
23+
formatter = ColoredLevelFormatter(tw, logfmt)
24+
output = formatter.format(record)
25+
assert output == (
26+
"dummypath 10 \x1b[32mINFO \x1b[0m Test Message"
27+
)
28+
29+
tw.hasmarkup = False
30+
formatter = ColoredLevelFormatter(tw, logfmt)
31+
output = formatter.format(record)
32+
assert output == ("dummypath 10 INFO Test Message")
33+
34+
35+
def test_coloredlogformatter_with_width_precision() -> None:
36+
logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8.8s %(message)s"
37+
38+
record = logging.LogRecord(
39+
name="dummy",
40+
level=logging.INFO,
41+
pathname="dummypath",
42+
lineno=10,
43+
msg="Test Message",
44+
args=(),
45+
exc_info=None,
46+
)
2447

2548
tw = TerminalWriter()
2649
tw.hasmarkup = True

0 commit comments

Comments
 (0)