2019-05-15 06:56:31 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-01-23 07:57:56 +08:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import py.io
|
2018-10-25 15:01:29 +08:00
|
|
|
|
2018-01-23 07:57:56 +08:00
|
|
|
from _pytest.logging import ColoredLevelFormatter
|
|
|
|
|
|
|
|
|
|
|
|
def test_coloredlogformatter():
|
2018-05-23 22:48:46 +08:00
|
|
|
logfmt = "%(filename)-25s %(lineno)4d %(levelname)-8s %(message)s"
|
2018-01-23 07:57:56 +08:00
|
|
|
|
|
|
|
record = logging.LogRecord(
|
2018-05-23 22:48:46 +08:00
|
|
|
name="dummy",
|
|
|
|
level=logging.INFO,
|
|
|
|
pathname="dummypath",
|
|
|
|
lineno=10,
|
|
|
|
msg="Test Message",
|
|
|
|
args=(),
|
|
|
|
exc_info=False,
|
|
|
|
)
|
2018-01-23 07:57:56 +08:00
|
|
|
|
|
|
|
class ColorConfig(object):
|
|
|
|
class option(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
tw = py.io.TerminalWriter()
|
|
|
|
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")
|