import os
import re
import time
import logging
def split_log_file(log_file_path, chunk_size_bytes, output_dir, max_retries=3):
"""
Splits a log file into smaller files with retry logic.
Args:
log_file_path (str): Path to the log file.
chunk_size_bytes (int): Size of each chunk in bytes.
output_dir (str): Directory to save the split files.
max_retries (int): Maximum number of retries for each split attempt.
"""
if not os.path.exists(output_dir):
os.makedirs(output_dir)
file_name = os.path.basename(log_file_path)
file_name_without_ext = os.path.splitext(file_name)[0]
for attempt in range(max_retries):
try:
with open(log_file_path, 'r') as infile:
outfile = None
outfile_handle = None
chunk_num = 1
for line in infile:
if outfile is None:
output_file_path = os.path.join(output_dir, f"{file_name_without_ext}_part{chunk_num}.log")
outfile = open(output_file_path, 'w')
outfile_handle = outfile
chunk_num += 1
if len(outfile.getvalue()) > chunk_size_bytes:
outfile_handle.close()
outfile = open(output_file_path, 'w')
outfile_handle = outfile
chunk_num = 1
outfile_handle.write(line)
if outfile_handle:
outfile_handle.close()
print(f"Successfully split {file_name} into chunks.")
return # Exit if successful
except Exception as e:
logging.error(f"Attempt {attempt + 1} failed: {e}")
if attempt < max_retries - 1:
print(f"Retrying split of {file_name} (attempt {attempt + 1}/{max_retries})...")
time.sleep(2) # Wait before retrying
else:
print(f"Failed to split {file_name} after {max_retries} attempts.")
return
Add your comment