-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlog_setup.py
100 lines (78 loc) · 2.96 KB
/
log_setup.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import logging
import logging.handlers
import time
from typing import Callable, List, Optional, TextIO, Tuple, Type, Union
import warnings
import static_config
logging.basicConfig(handlers=[], force=True)
def closure() -> None:
old_showwarning = warnings.showwarning
def showwarning(
message: Union[Warning, str],
category: Type[Warning],
filename: str,
lineno: int,
file: Optional[TextIO] = None,
line: Optional[str] = None,
) -> None:
if file is not None:
old_showwarning(message, category, filename, lineno, file, line)
else:
text = warnings.formatwarning(message, category, filename, lineno, line)
logging.getLogger("__builtins__").error(text)
warnings.showwarning = showwarning
closure()
logger: logging.Logger = logging.getLogger()
logger.setLevel(logging.NOTSET)
class Formatter(logging.Formatter):
"""A formatter that formats multi-line messages in a greppable fashion"""
__slots__ = ()
converter = time.gmtime
default_time_format = "%Y-%m-%dT%H:%M:%S"
default_msec_format = "%s.%03d"
def format(self, record: logging.LogRecord) -> str:
record.asctime = self.formatTime(record, self.datefmt)
if record.exc_info:
if not record.exc_text:
record.exc_text = self.formatException(record.exc_info)
lines = record.getMessage().split("\n")
if record.exc_text:
lines.extend(record.exc_text.split("\n"))
if record.stack_info:
lines.extend(self.formatStack(record.stack_info).split("\n"))
lines = list(filter(bool, lines))
output: List[str] = []
for i in range(len(lines)):
record.message = lines[i]
if len(lines) == 1:
record.symbol = ":"
elif i == 0:
record.symbol = "{"
elif i == len(lines) - 1:
record.symbol = "}"
else:
record.symbol = "|"
output.append(self.formatMessage(record))
return "\n".join(output)
formatter: logging.Formatter = Formatter("%(asctime)s %(name)s %(levelname)s%(symbol)s %(message)s")
targets: List[Tuple[int, str, Optional[Callable[[logging.LogRecord], bool]]]] = [
(logging.DEBUG, "debug.discord", lambda r: r.name.startswith("discord.")),
(logging.DEBUG, "debug", lambda r: not r.name.startswith("discord.")),
(logging.INFO, "info", None),
(logging.WARNING, "warning", None),
(logging.ERROR, "error", None),
(logging.CRITICAL, "critical", None),
]
for level, name, cond in targets:
handler = logging.handlers.TimedRotatingFileHandler(
filename="{}/{}.log".format(static_config.Log["directory"], name),
when="midnight",
utc=True,
encoding="utf",
errors="replace",
)
handler.setLevel(level)
handler.setFormatter(formatter)
if cond:
handler.addFilter(cond)
logger.addHandler(handler)