修改费用计算代码

This commit is contained in:
chentianrui
2025-08-22 18:13:09 +08:00
parent 8d595b339c
commit be848c3e78
20 changed files with 2569 additions and 360 deletions
+24 -22
View File
@@ -1,6 +1,7 @@
import re
import codecs
def extract_errors_and_warnings(input_log_path, output_error_path, warning_stats_path="warning_statistics.txt"):
"""
从日志文件中提取 WARNING 和 ERROR 及其 Traceback 堆栈信息,保存到新文件
@@ -8,28 +9,28 @@ def extract_errors_and_warnings(input_log_path, output_error_path, warning_stats
同时统计WARNING信息并输出到单独文件
"""
# 正则匹配日志行开头(时间戳格式)
log_pattern = re.compile(r'^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3})')
log_pattern = re.compile(r"^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3})")
# 尝试多种编码格式读取文件
encodings = ['utf-8', 'gbk', 'gb2312', 'ascii']
encodings = ["utf-8", "gbk", "gb2312", "ascii"]
lines = []
for encoding in encodings:
try:
with open(input_log_path, 'r', encoding=encoding) as f:
with open(input_log_path, "r", encoding=encoding) as f:
lines = f.readlines()
print(f"✅ 成功使用 {encoding} 编码读取文件")
break
except UnicodeDecodeError:
continue
# 如果所有编码都失败,则使用二进制模式读取并尝试解码
if not lines:
try:
with open(input_log_path, 'rb') as f:
with open(input_log_path, "rb") as f:
content = f.read()
# 尝试解码,忽略错误
lines = content.decode('utf-8', errors='ignore').splitlines(True)
lines = content.decode("utf-8", errors="ignore").splitlines(True)
print("⚠️ 使用二进制模式读取文件,可能有字符丢失")
except Exception as e:
print(f"❌ 无法读取文件: {e}")
@@ -46,10 +47,10 @@ def extract_errors_and_warnings(input_log_path, output_error_path, warning_stats
if is_new_log:
# 判断是否为 WARNING 或 ERROR
if ' - WARNING - ' in line or ' - ERROR - ' in line:
if " - WARNING - " in line or " - ERROR - " in line:
error_lines.append(line.rstrip())
# 如果是 ERROR,捕获后续的 Traceback 信息
if ' - ERROR - ' in line:
if " - ERROR - " in line:
i += 1
# 继续读取后续行,直到遇到下一个时间戳行或文件结束
while i < len(lines):
@@ -65,7 +66,7 @@ def extract_errors_and_warnings(input_log_path, output_error_path, warning_stats
error_lines.append(next_line.rstrip())
i += 1
# 如果是DEBUG/INFO行,检查是否包含Traceback
elif ' - DEBUG - ' in line and i + 1 < len(lines) and 'Traceback' in lines[i + 1]:
elif " - DEBUG - " in line and i + 1 < len(lines) and "Traceback" in lines[i + 1]:
# 这是一个包含Traceback的DEBUG信息,也提取
error_lines.append(line.rstrip())
i += 1
@@ -92,35 +93,36 @@ def extract_errors_and_warnings(input_log_path, output_error_path, warning_stats
i += 1
# 写入输出文件
with open(output_error_path, 'w', encoding='utf-8') as f:
with open(output_error_path, "w", encoding="utf-8") as f:
for err_line in error_lines:
f.write(err_line + '\n')
f.write(err_line + "\n")
# 统计WARNING信息
warning_dict = {}
for line in error_lines:
if ' - WARNING - ' in line:
if " - WARNING - " in line:
# 提取WARNING后的内容作为键
warning_content = line.split(' - WARNING - ', 1)[1]
warning_content = line.split(" - WARNING - ", 1)[1]
if warning_content in warning_dict:
warning_dict[warning_content] += 1
else:
warning_dict[warning_content] = 1
# 写入统计结果到文件
with open(warning_stats_path, 'w', encoding='utf-8') as f:
with open(warning_stats_path, "w", encoding="utf-8") as f:
f.write("WARNING统计结果:\n")
f.write(f"共找到 {len(warning_dict)} 种不同的WARNING信息\n\n")
for warning_content, count in warning_dict.items():
f.write(f"{count}次: {warning_content}\n")
f.write(f"{warning_content}\n")
print(f"✅ 提取完成!共找到 {len(error_lines)} 行错误/警告信息。")
print(f"📁 已保存到: {output_error_path}")
print(f"📊 WARNING统计已保存到: {warning_stats_path}")
# ============ 使用示例 ============
if __name__ == "__main__":
input_file = "bcl_calculator.log" # 替换为你的日志文件路径
output_file = "error_report.txt" # 输出的错误报告文件
warning_stats_file = "warning_statistics.txt" # WARNING统计结果文件
extract_errors_and_warnings(input_file, output_file, warning_stats_file)
input_file = "bcl_calculator.log" # 替换为你的日志文件路径
output_file = "error_report.txt" # 输出的错误报告文件
warning_stats_file = "warning_statistics.txt" # WARNING统计结果文件
extract_errors_and_warnings(input_file, output_file, warning_stats_file)