File tree Expand file tree Collapse file tree 1 file changed +12
-5
lines changed
Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Original file line number Diff line number Diff line change 3535def 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
4650def 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
7885def print_system_info ():
7986 """打印系统信息"""
You can’t perform that action at this time.
0 commit comments