rich/examples/log.py

78 lines
1.9 KiB
Python
Raw Permalink Normal View History

2020-03-10 22:18:23 +00:00
"""
A simulation of Rich console logging.
"""
2019-12-18 16:46:24 +00:00
import time
from rich.console import Console
from rich.style import Style
2020-04-26 15:28:18 +00:00
from rich.theme import Theme
2019-12-18 16:46:24 +00:00
from rich.highlighter import RegexHighlighter
class RequestHighlighter(RegexHighlighter):
base_style = "req."
highlights = [
r"^(?P<protocol>\w+) (?P<method>\w+) (?P<path>\S+) (?P<result>\w+) (?P<stats>\[.+\])$",
r"\/(?P<filename>\w+\..{3,4})",
]
2020-04-26 15:28:18 +00:00
theme = Theme(
2019-12-18 16:46:24 +00:00
{
"req.protocol": Style.parse("dim bold green"),
"req.method": Style.parse("bold cyan"),
"req.path": Style.parse("magenta"),
"req.filename": Style.parse("bright_magenta"),
"req.result": Style.parse("yellow"),
"req.stats": Style.parse("dim"),
}
)
2020-04-26 15:28:18 +00:00
console = Console(theme=theme)
2019-12-18 16:46:24 +00:00
console.log("Server starting...")
2020-01-11 16:13:39 +00:00
console.log("Serving on http://127.0.0.1:8000")
2019-12-18 16:46:24 +00:00
time.sleep(1)
2020-01-26 16:09:22 +00:00
request_highlighter = RequestHighlighter()
2019-12-18 16:46:24 +00:00
console.log(
2020-01-26 16:09:22 +00:00
request_highlighter("HTTP GET /foo/bar/baz/egg.html 200 [0.57, 127.0.0.1:59076]"),
2019-12-18 16:46:24 +00:00
)
console.log(
2020-01-26 16:09:22 +00:00
request_highlighter(
"HTTP GET /foo/bar/baz/background.jpg 200 [0.57, 127.0.0.1:59076]"
),
2019-12-18 16:46:24 +00:00
)
time.sleep(1)
def test_locals():
foo = (1, 2, 3)
movies = ["Deadpool", "Rise of the Skywalker"]
console = Console()
console.log(
2019-12-21 22:24:15 +00:00
"[b]JSON[/b] RPC [i]batch[/i]",
2019-12-18 16:46:24 +00:00
[
{"jsonrpc": "2.0", "method": "sum", "params": [1, 2, 4], "id": "1"},
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": "2"},
{"foo": "boo"},
{
"jsonrpc": "2.0",
"method": "foo.get",
"params": {"name": "myself", "enable": False, "grommits": None},
"id": "5",
},
{"jsonrpc": "2.0", "method": "get_data", "id": "9"},
],
log_locals=True,
)
test_locals()