Skip to content

Commit 2240ce4

Browse files
committed
feat[log]:add error log to file
1 parent 26611bf commit 2240ce4

File tree

11 files changed

+1296
-33
lines changed

11 files changed

+1296
-33
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ dmypy.json
121121
FETCH_HEAD
122122

123123
#log
124-
log*/
124+
log/
125125

126126
checkpoints/
127127
checkpoints_origin/

fastdeploy/logger/__init__.py

Whitespace-only changes.

fastdeploy/logger/formatters.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
自定义日志格式化器模块
3+
该模块定义了 ColoredFormatter 类,用于在控制台输出带颜色的日志信息,
4+
便于开发者在终端中快速识别不同级别的日志。
5+
"""
6+
7+
import logging
8+
9+
10+
class ColoredFormatter(logging.Formatter):
11+
"""
12+
自定义日志格式器,用于控制台输出带颜色的日志。
13+
支持的颜色:
14+
- WARNING: 黄色
15+
- ERROR: 红色
16+
- CRITICAL: 红色
17+
- 其他等级: 默认终端颜色
18+
"""
19+
20+
COLOR_CODES = {
21+
logging.WARNING: 33, # 黄色
22+
logging.ERROR: 31, # 红色
23+
logging.CRITICAL: 31, # 红色
24+
}
25+
26+
def format(self, record):
27+
"""
28+
格式化日志记录,并根据日志等级添加 ANSI 颜色前缀和后缀。
29+
Args:
30+
record (LogRecord): 日志记录对象。
31+
Returns:
32+
str: 带有颜色的日志消息字符串。
33+
"""
34+
color_code = self.COLOR_CODES.get(record.levelno, 0)
35+
prefix = f"\033[{color_code}m"
36+
suffix = "\033[0m"
37+
message = super().format(record)
38+
if color_code:
39+
message = f"{prefix}{message}{suffix}"
40+
return message

0 commit comments

Comments
 (0)