forked from Ikaros-521/AI-Vtuber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
39 lines (31 loc) · 1.15 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
import logging
import colorlog
def Configure_logger(log_file):
log_format = '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
color_format = '%(log_color)s%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s%(reset)s'
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# 创建一个处理程序
handler = logging.FileHandler(log_file, encoding='utf-8', mode='a+')
handlers = [handler]
# 创建控制台处理程序并设置颜色
console = colorlog.StreamHandler()
console.setFormatter(colorlog.ColoredFormatter(
color_format,
datefmt='%Y-%m-%d %H:%M:%S',
log_colors={
'DEBUG': 'cyan',
'INFO': 'white', # 将INFO的颜色设置为白色
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
}
))
handlers.append(console)
logger.handlers = handlers
formatter = colorlog.ColoredFormatter(
color_format,
datefmt='%Y-%m-%d %H:%M:%S'
)
# 将处理程序添加到记录器,并设置格式化器
handler.setFormatter(formatter)