2018-01-23 07:57:56 +08:00
|
|
|
import logging
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import Any
|
2018-01-23 07:57:56 +08:00
|
|
|
|
2020-01-23 19:45:38 +08:00
|
|
|
from _pytest._io import TerminalWriter
|
2018-01-23 07:57:56 +08:00
|
|
|
from _pytest.logging import ColoredLevelFormatter
|
|
|
|
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_coloredlogformatter() -> None:
|
2018-01-23 07:57:56 +08:00
|
|
|
logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8s %(message)s"
|
|
|
|
|
|
|
|
record = logging.LogRecord(
|
|
|
|
name="dummy",
|
|
|
|
level=logging.INFO,
|
|
|
|
pathname="dummypath",
|
|
|
|
lineno=10,
|
|
|
|
msg="Test Message",
|
|
|
|
args=(),
|
2020-05-01 19:40:17 +08:00
|
|
|
exc_info=None,
|
2018-01-23 07:57:56 +08:00
|
|
|
)
|
|
|
|
|
2020-01-23 19:45:38 +08:00
|
|
|
tw = TerminalWriter()
|
2018-01-23 07:57:56 +08:00
|
|
|
tw.hasmarkup = True
|
|
|
|
formatter = ColoredLevelFormatter(tw, logfmt)
|
|
|
|
output = formatter.format(record)
|
2018-06-26 21:35:27 +08:00
|
|
|
assert output == (
|
2018-10-11 01:28:31 +08:00
|
|
|
"dummypath 10 \x1b[32mINFO \x1b[0m Test Message"
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2018-01-23 07:57:56 +08:00
|
|
|
|
|
|
|
tw.hasmarkup = False
|
|
|
|
formatter = ColoredLevelFormatter(tw, logfmt)
|
|
|
|
output = formatter.format(record)
|
2018-10-11 01:28:31 +08:00
|
|
|
assert output == ("dummypath 10 INFO Test Message")
|
2019-05-24 10:32:22 +08:00
|
|
|
|
|
|
|
|
2021-05-07 18:46:40 +08:00
|
|
|
def test_coloredlogformatter_with_width_precision() -> None:
|
2021-05-07 18:55:19 +08:00
|
|
|
logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8.8s %(message)s"
|
2021-05-07 18:46:40 +08:00
|
|
|
|
|
|
|
record = logging.LogRecord(
|
|
|
|
name="dummy",
|
|
|
|
level=logging.INFO,
|
|
|
|
pathname="dummypath",
|
|
|
|
lineno=10,
|
|
|
|
msg="Test Message",
|
|
|
|
args=(),
|
|
|
|
exc_info=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
tw = TerminalWriter()
|
|
|
|
tw.hasmarkup = True
|
|
|
|
formatter = ColoredLevelFormatter(tw, logfmt)
|
|
|
|
output = formatter.format(record)
|
|
|
|
assert output == (
|
|
|
|
"dummypath 10 \x1b[32mINFO \x1b[0m Test Message"
|
|
|
|
)
|
|
|
|
|
|
|
|
tw.hasmarkup = False
|
|
|
|
formatter = ColoredLevelFormatter(tw, logfmt)
|
|
|
|
output = formatter.format(record)
|
|
|
|
assert output == ("dummypath 10 INFO Test Message")
|
|
|
|
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_multiline_message() -> None:
|
2019-05-24 10:32:22 +08:00
|
|
|
from _pytest.logging import PercentStyleMultiline
|
|
|
|
|
|
|
|
logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8s %(message)s"
|
|
|
|
|
2020-10-06 09:13:05 +08:00
|
|
|
record: Any = logging.LogRecord(
|
2019-05-24 10:32:22 +08:00
|
|
|
name="dummy",
|
|
|
|
level=logging.INFO,
|
|
|
|
pathname="dummypath",
|
|
|
|
lineno=10,
|
|
|
|
msg="Test Message line1\nline2",
|
|
|
|
args=(),
|
2020-05-01 19:40:17 +08:00
|
|
|
exc_info=None,
|
2020-10-06 09:13:05 +08:00
|
|
|
)
|
2019-05-24 10:32:22 +08:00
|
|
|
# this is called by logging.Formatter.format
|
|
|
|
record.message = record.getMessage()
|
|
|
|
|
2019-10-08 05:56:28 +08:00
|
|
|
ai_on_style = PercentStyleMultiline(logfmt, True)
|
|
|
|
output = ai_on_style.format(record)
|
2019-05-24 10:32:22 +08:00
|
|
|
assert output == (
|
|
|
|
"dummypath 10 INFO Test Message line1\n"
|
|
|
|
" line2"
|
|
|
|
)
|
2019-05-30 05:15:34 +08:00
|
|
|
|
2019-10-08 05:56:28 +08:00
|
|
|
ai_off_style = PercentStyleMultiline(logfmt, False)
|
|
|
|
output = ai_off_style.format(record)
|
|
|
|
assert output == (
|
|
|
|
"dummypath 10 INFO Test Message line1\nline2"
|
|
|
|
)
|
|
|
|
|
|
|
|
ai_none_style = PercentStyleMultiline(logfmt, None)
|
|
|
|
output = ai_none_style.format(record)
|
|
|
|
assert output == (
|
|
|
|
"dummypath 10 INFO Test Message line1\nline2"
|
|
|
|
)
|
|
|
|
|
|
|
|
record.auto_indent = False
|
|
|
|
output = ai_on_style.format(record)
|
|
|
|
assert output == (
|
|
|
|
"dummypath 10 INFO Test Message line1\nline2"
|
|
|
|
)
|
|
|
|
|
|
|
|
record.auto_indent = True
|
|
|
|
output = ai_off_style.format(record)
|
|
|
|
assert output == (
|
|
|
|
"dummypath 10 INFO Test Message line1\n"
|
|
|
|
" line2"
|
|
|
|
)
|
|
|
|
|
|
|
|
record.auto_indent = "False"
|
|
|
|
output = ai_on_style.format(record)
|
|
|
|
assert output == (
|
|
|
|
"dummypath 10 INFO Test Message line1\nline2"
|
|
|
|
)
|
|
|
|
|
|
|
|
record.auto_indent = "True"
|
|
|
|
output = ai_off_style.format(record)
|
|
|
|
assert output == (
|
|
|
|
"dummypath 10 INFO Test Message line1\n"
|
|
|
|
" line2"
|
|
|
|
)
|
|
|
|
|
|
|
|
# bad string values default to False
|
|
|
|
record.auto_indent = "junk"
|
|
|
|
output = ai_off_style.format(record)
|
|
|
|
assert output == (
|
|
|
|
"dummypath 10 INFO Test Message line1\nline2"
|
|
|
|
)
|
|
|
|
|
|
|
|
# anything other than string or int will default to False
|
|
|
|
record.auto_indent = dict()
|
|
|
|
output = ai_off_style.format(record)
|
|
|
|
assert output == (
|
|
|
|
"dummypath 10 INFO Test Message line1\nline2"
|
|
|
|
)
|
|
|
|
|
|
|
|
record.auto_indent = "5"
|
|
|
|
output = ai_off_style.format(record)
|
|
|
|
assert output == (
|
|
|
|
"dummypath 10 INFO Test Message line1\n line2"
|
|
|
|
)
|
|
|
|
|
|
|
|
record.auto_indent = 5
|
|
|
|
output = ai_off_style.format(record)
|
|
|
|
assert output == (
|
|
|
|
"dummypath 10 INFO Test Message line1\n line2"
|
|
|
|
)
|
|
|
|
|
2019-05-30 05:15:34 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_colored_short_level() -> None:
|
2019-05-30 05:15:34 +08:00
|
|
|
logfmt = "%(levelname).1s %(message)s"
|
|
|
|
|
|
|
|
record = logging.LogRecord(
|
|
|
|
name="dummy",
|
|
|
|
level=logging.INFO,
|
|
|
|
pathname="dummypath",
|
|
|
|
lineno=10,
|
|
|
|
msg="Test Message",
|
|
|
|
args=(),
|
2020-05-01 19:40:17 +08:00
|
|
|
exc_info=None,
|
2019-05-30 05:15:34 +08:00
|
|
|
)
|
|
|
|
|
2019-06-03 23:08:50 +08:00
|
|
|
class ColorConfig:
|
|
|
|
class option:
|
2019-05-30 05:15:34 +08:00
|
|
|
pass
|
|
|
|
|
2020-01-23 19:45:38 +08:00
|
|
|
tw = TerminalWriter()
|
2019-05-30 05:15:34 +08:00
|
|
|
tw.hasmarkup = True
|
|
|
|
formatter = ColoredLevelFormatter(tw, logfmt)
|
|
|
|
output = formatter.format(record)
|
|
|
|
# the I (of INFO) is colored
|
|
|
|
assert output == ("\x1b[32mI\x1b[0m Test Message")
|