-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
89 lines (73 loc) · 2.67 KB
/
logger.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
# coding=utf-8
import logging
from colorlog import ColoredFormatter
import os
import datetime
import zoneinfo
base_dir = os.path.abspath(os.path.dirname(__file__))
now_tz = zoneinfo.ZoneInfo("Asia/Taipei")
class CreateLogger:
def __init__(self):
super().__init__()
self.c_logger = self.color_logger()
self.a_logger = self.anonymous_logger()
logging.addLevelName(25, "ANONYMOUS")
logging.addLevelName(35, "BUGFLAG")
@staticmethod
def color_logger():
formatter = ColoredFormatter(
fmt="%(white)s[%(asctime)s] %(log_color)s%(levelname)-10s%(reset)s %(blue)s%(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
reset=True,
log_colors={
"DEBUG": "cyan",
"INFO": "green",
"ANONYMOUS": "purple",
"WARNING": "yellow",
"BUGFLAG": "cyan",
"ERROR": "red",
"CRITICAL": "red",
},
)
logger = logging.getLogger()
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
log_path = os.path.join(base_dir, "logs",
f"logs {datetime.datetime.now(tz=now_tz).strftime('%Y.%m.%d %H.%M.%S')}.log")
with open(log_path, "w"):
pass
f_formatter = logging.Formatter(
fmt="[%(asctime)s] %(levelname)-10s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S")
f_handler = logging.FileHandler(log_path, encoding="utf-8")
f_handler.setFormatter(f_formatter)
logger.addHandler(f_handler)
logger.setLevel(logging.DEBUG)
return logger
@staticmethod
def anonymous_logger():
formatter = logging.Formatter(
fmt="[%(asctime)s] %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S")
logger = logging.getLogger("anonymous_logger")
handler = logging.FileHandler("anonymous.log", encoding="utf-8")
handler.setLevel(25)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
def debug(self, message: str):
self.c_logger.debug(message)
def info(self, message: str):
self.c_logger.info(message)
def warning(self, message: str):
self.c_logger.warning(message)
def error(self, message: str):
self.c_logger.error(message)
def critical(self, message: str):
self.c_logger.critical(message)
def anonymous(self, message: str):
self.c_logger.log(25, message)
self.a_logger.log(25, message)
def bugflag(self, message: str):
self.c_logger.log(35, message)