# 验证日志文件是否存在 log_file = Path(log_path) if not log_file.exists() or not log_file.is_file(): print(f"错误:日志文件不存在或不是文件 -> {log_path}") sys.exit(1)
# 统计匹配行数 match_count = 0
try: # 逐行读取日志(低内存占用,支持超大文件) with open(log_file, "r", encoding="utf-8", errors="ignore") as infile, \ open(output_path, "w", encoding="utf-8") as outfile:
for line in infile: # 核心匹配:日志行以 "2025-11-27 " 开头(日期后带空格,适配 catalina.out 格式) if line.startswith(f"{target_date} "): outfile.write(line) match_count += 1