import time
import logging
def stream_log_files(log_file_paths, interval=1):
"""
Streams data from multiple log files synchronously.
Args:
log_file_paths (list): A list of paths to log files.
interval (int): The time interval (in seconds) between log file reads.
"""
logging.basicConfig(level=logging.INFO) # Configure basic logging
while True:
for file_path in log_file_paths:
try:
with open(file_path, 'r') as f:
for line in f:
logging.info(f"[{file_path}] {line.strip()}") # Log each line with file prefix
except FileNotFoundError:
logging.warning(f"Log file not found: {file_path}")
except Exception as e:
logging.error(f"Error reading {file_path}: {e}")
time.sleep(interval) # Wait before reading again
if __name__ == '__main__':
# Example usage
log_files = ["log1.txt", "log2.txt"] # Replace with your log file paths
# Create dummy log files for testing
with open("log1.txt", "w") as f:
f.write("2023-10-27 10:00:00 - INFO - Starting process\n")
f.write("2023-10-27 10:00:05 - DEBUG - Debugging information\n")
with open("log2.txt", "w") as f:
f.write("2023-10-27 10:00:10 - WARNING - Potential issue\n")
f.write("2023-10-27 10:00:15 - INFO - Process completed\n")
stream_log_files(log_files, interval=5) # Stream logs every 5 seconds
Add your comment