-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathcli.py
More file actions
85 lines (74 loc) · 2.49 KB
/
cli.py
File metadata and controls
85 lines (74 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import logging
from typing import Any
from rich_toolkit import RichToolkit, RichToolkitTheme
from rich_toolkit.styles import TaggedStyle
from uvicorn.logging import DefaultFormatter
class CustomFormatter(DefaultFormatter):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.toolkit = get_rich_toolkit()
def formatMessage(self, record: logging.LogRecord) -> str:
message = record.getMessage()
result = self.toolkit.print_as_string(message, tag=record.levelname)
# Prepend newline to fix alignment after ^C is printed by the terminal
if message == "Shutting down":
result = "\n" + result
return result
def get_uvicorn_log_config() -> dict[str, Any]:
return {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"default": {
"()": CustomFormatter,
"fmt": "%(levelprefix)s %(message)s",
"use_colors": None,
},
"access": {
"()": CustomFormatter,
"fmt": "%(levelprefix)s %(client_addr)s - '%(request_line)s' %(status_code)s",
},
},
"handlers": {
"default": {
"formatter": "default",
"class": "logging.StreamHandler",
"stream": "ext://sys.stderr",
},
"access": {
"formatter": "access",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
},
"loggers": {
"uvicorn": {
"handlers": ["default"],
"level": "INFO",
"propagate": False,
},
"uvicorn.error": {"level": "INFO"},
"uvicorn.access": {
"handlers": ["access"],
"level": "INFO",
"propagate": False,
},
},
}
logger = logging.getLogger(__name__)
def get_rich_toolkit() -> RichToolkit:
theme = RichToolkitTheme(
style=TaggedStyle(tag_width=11),
theme={
"tag.title": "white on #009485",
"tag": "white on #007166",
"placeholder": "grey85",
"text": "white",
"selected": "#007166",
"result": "grey85",
"progress": "on #007166",
"error": "red",
"log.info": "black on blue",
},
)
return RichToolkit(theme=theme)