Skip to content

Commit 7e1b606

Browse files
修复Windows编码问题:将全角分隔符改为半角,添加Unicode错误处理
1 parent 26df51c commit 7e1b606

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

main.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@
3535
def log(message, level="info"):
3636
"""
3737
记录日志
38-
格式: 时间(日期+时间)类型(info/error/warning)内容
38+
格式: 时间(日期+时间)| 类型(info/error/warning)| 内容
3939
"""
4040
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
41-
log_entry = f"{timestamp}{level}{message}"
41+
log_entry = f"{timestamp} | {level} | {message}"
4242
log_entries.append(log_entry)
4343
# 同时输出到控制台
44-
print(log_entry)
44+
try:
45+
print(log_entry)
46+
except UnicodeEncodeError:
47+
# Windows 控制台编码问题,使用 ASCII 字符
48+
print(f"{timestamp} | {level} | {message.encode('ascii', 'replace').decode('ascii')}")
4549

4650
def save_logs():
4751
"""
@@ -69,11 +73,14 @@ def save_logs():
6973

7074
# 写入日志
7175
with open(filename, 'w', encoding='utf-8') as f:
72-
f.write("时间(日期+时间)类型(info/error/warning)内容\n")
76+
f.write("时间(日期+时间)| 类型(info/error/warning)| 内容\n")
7377
for entry in log_entries:
7478
f.write(entry + "\n")
7579

76-
print(f"日志已保存到: {filename}")
80+
try:
81+
print(f"日志已保存到: {filename}")
82+
except UnicodeEncodeError:
83+
print(f"Logs saved to: {filename}")
7784

7885
def print_system_info():
7986
"""打印系统信息"""

0 commit comments

Comments
 (0)