-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogUtil.py
executable file
·46 lines (40 loc) · 1.42 KB
/
logUtil.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
import logging
import logging.config
green = "\x1b[32;20m"
grey = "\x1b[38;20m"
blue = "\x1b[34;20m"
yellow = "\x1b[33;20m"
red = "\x1b[31;20m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
fmt = "%(asctime)s %(levelname)s %(name)s "
class CustomFormatter(logging.Formatter):
FORMATS = {
logging.DEBUG: blue + fmt + reset + "%(message)s",
logging.INFO: green + fmt + reset + "%(message)s",
logging.WARNING: yellow + fmt + reset + "%(message)s",
logging.ERROR: red + fmt + reset + "%(message)s",
logging.CRITICAL: bold_red + fmt + reset + "%(message)s",
logging.NOTSET: fmt + "%(message)s"
}
def format(self, record):
try:
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
except Exception:
fmt = "%(asctime)s %(levelname)s %(name)s %(message)s"
return logging.Formatter(fmt).format(record)
def initLog(file: str, level=logging.INFO):
name = file.split("/src/").pop()
if (name == "" or name is None):
name = file
logger = logging.getLogger(name)
logger.setLevel(level)
formatter = CustomFormatter()
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
for handler in logger.handlers:
print(f"logger Handler for file={file} => {handler}")
return logger