| 1 |
""" |
| 2 |
A simulation of Rich console logging. |
| 3 |
""" |
| 4 |
|
| 5 |
import time |
| 6 |
from rich.console import Console |
| 7 |
from rich.style import Style |
| 8 |
from rich.theme import Theme |
| 9 |
from rich.highlighter import RegexHighlighter |
| 10 |
|
| 11 |
|
| 12 |
class RequestHighlighter(RegexHighlighter): |
| 13 |
base_style = "req." |
| 14 |
highlights = [ |
| 15 |
r"^(?P<protocol>\w+) (?P<method>\w+) (?P<path>\S+) (?P<result>\w+) (?P<stats>\[.+\])$", |
| 16 |
r"\/(?P<filename>\w+\..{3,4})", |
| 17 |
] |
| 18 |
|
| 19 |
|
| 20 |
theme = Theme( |
| 21 |
{ |
| 22 |
"req.protocol": Style.parse("dim bold green"), |
| 23 |
"req.method": Style.parse("bold cyan"), |
| 24 |
"req.path": Style.parse("magenta"), |
| 25 |
"req.filename": Style.parse("bright_magenta"), |
| 26 |
"req.result": Style.parse("yellow"), |
| 27 |
"req.stats": Style.parse("dim"), |
| 28 |
} |
| 29 |
) |
| 30 |
console = Console(theme=theme) |
| 31 |
|
| 32 |
console.log("Server starting...") |
| 33 |
console.log("Serving on http://127.0.0.1:8000") |
| 34 |
|
| 35 |
time.sleep(1) |
| 36 |
|
| 37 |
request_highlighter = RequestHighlighter() |
| 38 |
|
| 39 |
console.log( |
| 40 |
request_highlighter("HTTP GET /foo/bar/baz/egg.html 200 [0.57, 127.0.0.1:59076]"), |
| 41 |
) |
| 42 |
|
| 43 |
console.log( |
| 44 |
request_highlighter( |
| 45 |
"HTTP GET /foo/bar/baz/background.jpg 200 [0.57, 127.0.0.1:59076]" |
| 46 |
), |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
time.sleep(1) |
| 51 |
|
| 52 |
|
| 53 |
def test_locals(): |
| 54 |
foo = (1, 2, 3) |
| 55 |
movies = ["Deadpool", "Rise of the Skywalker"] |
| 56 |
console = Console() |
| 57 |
|
| 58 |
console.log( |
| 59 |
"[b]JSON[/b] RPC [i]batch[/i]", |
| 60 |
[ |
| 61 |
{"jsonrpc": "2.0", "method": "sum", "params": [1, 2, 4], "id": "1"}, |
| 62 |
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}, |
| 63 |
{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": "2"}, |
| 64 |
{"foo": "boo"}, |
| 65 |
{ |
| 66 |
"jsonrpc": "2.0", |
| 67 |
"method": "foo.get", |
| 68 |
"params": {"name": "myself", "enable": False, "grommits": None}, |
| 69 |
"id": "5", |
| 70 |
}, |
| 71 |
{"jsonrpc": "2.0", "method": "get_data", "id": "9"}, |
| 72 |
], |
| 73 |
log_locals=True, |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
test_locals() |