2020-04-26 11:38:38 +00:00
|
|
|
import io
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from rich.console import Console
|
|
|
|
from rich.logging import RichHandler
|
|
|
|
|
|
|
|
handler = RichHandler(
|
2020-06-14 14:23:43 +00:00
|
|
|
console=Console(
|
|
|
|
file=io.StringIO(), force_terminal=True, width=80, color_system="truecolor"
|
2020-06-14 14:38:45 +00:00
|
|
|
),
|
|
|
|
enable_link_path=False,
|
2020-04-26 11:38:38 +00:00
|
|
|
)
|
|
|
|
logging.basicConfig(
|
2020-05-13 09:06:11 +00:00
|
|
|
level="NOTSET", format="%(message)s", datefmt="[DATE]", handlers=[handler]
|
2020-04-26 11:38:38 +00:00
|
|
|
)
|
|
|
|
log = logging.getLogger("rich")
|
|
|
|
|
|
|
|
|
|
|
|
def make_log():
|
|
|
|
log.debug("foo")
|
|
|
|
render = handler.console.file.getvalue()
|
|
|
|
return render
|
|
|
|
|
|
|
|
|
|
|
|
def test_log():
|
|
|
|
render = make_log()
|
2020-06-14 13:51:19 +00:00
|
|
|
print(repr(render))
|
2020-06-14 14:38:45 +00:00
|
|
|
expected = "\x1b[2;36m[DATE]\x1b[0m\x1b[2;36m \x1b[0m\x1b[32mDEBUG\x1b[0m foo \x1b[2mtest_logging.py\x1b[0m\x1b[2m:20\x1b[0m\n"
|
2020-04-26 11:38:38 +00:00
|
|
|
assert render == expected
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
render = make_log()
|
|
|
|
print(render)
|
|
|
|
print(repr(render))
|