-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.py
More file actions
60 lines (50 loc) · 1.74 KB
/
log.py
File metadata and controls
60 lines (50 loc) · 1.74 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
import logging
import os
import sys
import re
from backupchan_server import utility
from dataclasses import dataclass
# TODO make configurable
LOG_DIRECTORY = utility.join_path(".", "log")
LOG_FILE = utility.join_path(LOG_DIRECTORY, "backupchan.log")
@dataclass
class LogLine:
time: str
module: str
level: str
message: str
def init():
if not os.path.isdir(LOG_DIRECTORY):
os.mkdir(LOG_DIRECTORY)
formatter = logging.Formatter("[%(asctime)s] [%(name)s] [%(levelname)s]: %(message)s")
file_handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes=2000000, backupCount=5)
file_handler.setFormatter(formatter)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(formatter)
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
root_logger.addHandler(file_handler)
root_logger.addHandler(console_handler)
def read(tail: int):
log_content = ""
with open(LOG_FILE, "r") as log_file:
if tail == 0:
log_content = log_file.read()
else:
log_content = "".join(log_file.readlines()[-tail:])
return log_content
def parse(log: str) -> list[LogLine]:
lines = log.split("\n")
log_lines = []
for line in lines:
if not re.match(r"\[\d+-\d+-\d+ \d+:\d+:\d+,\d+\] \[[a-z_.]+\] \[[A-Z]+\]: .*", line):
log_lines.append(LogLine("", "", "", line))
continue
print(line)
message = re.search(r": .*", line).group(0)[2:]
info_split = line.replace(message, "").split("] [")
time = info_split[0][1:]
module = info_split[1]
level = info_split[2][:-3]
log_lines.append(LogLine(time, module, level, message))
return log_lines