1. import os
  2. import re
  3. import time
  4. import logging
  5. def split_log_file(log_file_path, chunk_size_bytes, output_dir, max_retries=3):
  6. """
  7. Splits a log file into smaller files with retry logic.
  8. Args:
  9. log_file_path (str): Path to the log file.
  10. chunk_size_bytes (int): Size of each chunk in bytes.
  11. output_dir (str): Directory to save the split files.
  12. max_retries (int): Maximum number of retries for each split attempt.
  13. """
  14. if not os.path.exists(output_dir):
  15. os.makedirs(output_dir)
  16. file_name = os.path.basename(log_file_path)
  17. file_name_without_ext = os.path.splitext(file_name)[0]
  18. for attempt in range(max_retries):
  19. try:
  20. with open(log_file_path, 'r') as infile:
  21. outfile = None
  22. outfile_handle = None
  23. chunk_num = 1
  24. for line in infile:
  25. if outfile is None:
  26. output_file_path = os.path.join(output_dir, f"{file_name_without_ext}_part{chunk_num}.log")
  27. outfile = open(output_file_path, 'w')
  28. outfile_handle = outfile
  29. chunk_num += 1
  30. if len(outfile.getvalue()) > chunk_size_bytes:
  31. outfile_handle.close()
  32. outfile = open(output_file_path, 'w')
  33. outfile_handle = outfile
  34. chunk_num = 1
  35. outfile_handle.write(line)
  36. if outfile_handle:
  37. outfile_handle.close()
  38. print(f"Successfully split {file_name} into chunks.")
  39. return # Exit if successful
  40. except Exception as e:
  41. logging.error(f"Attempt {attempt + 1} failed: {e}")
  42. if attempt < max_retries - 1:
  43. print(f"Retrying split of {file_name} (attempt {attempt + 1}/{max_retries})...")
  44. time.sleep(2) # Wait before retrying
  45. else:
  46. print(f"Failed to split {file_name} after {max_retries} attempts.")
  47. return

Add your comment