forked from iMeanAI/WebCanvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.py
66 lines (55 loc) · 1.6 KB
/
logs.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
import logging
import os
import sys
import time
import colorlog
import re
log_folder = "LOGS"
if not os.path.exists(log_folder):
os.makedirs(log_folder)
log_file_name = os.path.join(
log_folder, time.strftime("%Y-%m-%d_%H-%M-%S") + ".log")
logger = logging.getLogger()
logger.setLevel(logging.INFO)
stream_formatter = colorlog.ColoredFormatter(
"%(asctime)s**[%(log_color)s%(levelname)s%(reset)s]**|| %(message)s",
datefmt=None,
reset=True,
log_colors={
'DEBUG': 'cyan',
'WARNING': 'yellow',
'ERROR': 'red',
'INFO': 'green',
'CRITICAL': 'red,bg_white',
},
secondary_log_colors={},
style='%'
)
class Formatter(colorlog.ColoredFormatter):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.color_pattern = re.compile(r'\x1b\[[0-9;]*m')
def format(self, record):
formatted_record = super().format(record)
clean_record = self.color_pattern.sub('', formatted_record)
return clean_record
file_formatter = Formatter(
"%(asctime)s**[%(levelname)s]**|| %(message)s",
datefmt=None,
reset=True,
log_colors={
'DEBUG': 'cyan',
'WARNING': 'yellow',
'ERROR': 'red',
'INFO': 'green',
'CRITICAL': 'red,bg_white',
},
secondary_log_colors={},
style='%'
)
file_handler = logging.FileHandler(log_file_name, encoding='utf-8')
file_handler.setFormatter(file_formatter)
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setFormatter(stream_formatter)
logger.addHandler(file_handler)
logger.addHandler(stream_handler)