extract_push_action_logs.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import re
  2. def extract_agent_run_steps(log_path):
  3. pattern = re.compile(
  4. r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} - agent run\[\d+\] - DEBUG - current step content:'
  5. )
  6. timestamp_pattern = re.compile(r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} - ')
  7. results = []
  8. current = []
  9. collecting = False
  10. with open(log_path, 'r', encoding='utf-8') as f:
  11. for line in f:
  12. if pattern.match(line):
  13. if collecting and current:
  14. results.append(''.join(current).rstrip())
  15. current = []
  16. collecting = True
  17. current.append(line)
  18. elif collecting:
  19. if timestamp_pattern.match(line):
  20. results.append(''.join(current).rstrip())
  21. current = []
  22. collecting = False
  23. else:
  24. current.append(line)
  25. # 文件结尾处理
  26. if collecting and current:
  27. results.append(''.join(current).rstrip())
  28. return results
  29. if __name__ == "__main__":
  30. import sys
  31. if len(sys.argv) != 2:
  32. print("Usage: python extract_agent_run_step.py <logfile>")
  33. sys.exit(1)
  34. log_file = sys.argv[1]
  35. steps = extract_agent_run_steps(log_file)
  36. for i, step in enumerate(steps, 1):
  37. print(f"--- Step {i} ---")
  38. print(step)
  39. print()