-
Notifications
You must be signed in to change notification settings - Fork 0
/
loggers.py
65 lines (52 loc) · 1.83 KB
/
loggers.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
import logging
import logging.handlers
from supports_ansi import stream_supports_colour
__all__ = ("setup_logging",)
class _ColorFormatter(logging.Formatter):
LEVEL_COLOURS = [
(logging.DEBUG, "\x1b[40;1m"),
(logging.INFO, "\x1b[34;1m"),
(logging.WARNING, "\x1b[33;1m"),
(logging.ERROR, "\x1b[31m"),
(logging.CRITICAL, "\x1b[41m"),
]
FORMATS = {
level: logging.Formatter(
f"\x1b[30;1m%(asctime)s\x1b[0m {colour}%(levelname)-8s\x1b[0m \x1b[33m%(name)s\x1b[0m %(message)s",
"%Y-%m-%d %H:%M:%S",
)
for level, colour in LEVEL_COLOURS
}
def format(self, record):
formatter = self.FORMATS.get(record.levelno)
if formatter is None:
formatter = self.FORMATS[logging.DEBUG]
if record.exc_info:
text = formatter.formatException(record.exc_info)
record.exc_text = f"\x1b[31m{text}\x1b[0m"
output = formatter.format(record)
record.exc_text = None
return output
def setup_logging(debug: bool):
logger = logging.getLogger("uptime")
if debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
# Setting up file handler
handler = logging.handlers.RotatingFileHandler(
filename="logs.log", encoding="utf-8", maxBytes=32 * 1024 * 1024, backupCount=5
)
dt_fmt = "%Y-%m-%d %H:%M:%S"
formatter = logging.Formatter(
"[{asctime}] [{levelname:<8}] {name}: {message}", dt_fmt, style="{"
)
handler.setFormatter(formatter)
logger.addHandler(handler)
# Setting up console handler
log_handler = logging.StreamHandler()
if stream_supports_colour(log_handler.stream):
formatter = _ColorFormatter()
log_handler.setFormatter(formatter)
logger.addHandler(log_handler)
return logger