1. import time
  2. import logging
  3. def stream_log_files(log_file_paths, interval=1):
  4. """
  5. Streams data from multiple log files synchronously.
  6. Args:
  7. log_file_paths (list): A list of paths to log files.
  8. interval (int): The time interval (in seconds) between log file reads.
  9. """
  10. logging.basicConfig(level=logging.INFO) # Configure basic logging
  11. while True:
  12. for file_path in log_file_paths:
  13. try:
  14. with open(file_path, 'r') as f:
  15. for line in f:
  16. logging.info(f"[{file_path}] {line.strip()}") # Log each line with file prefix
  17. except FileNotFoundError:
  18. logging.warning(f"Log file not found: {file_path}")
  19. except Exception as e:
  20. logging.error(f"Error reading {file_path}: {e}")
  21. time.sleep(interval) # Wait before reading again
  22. if __name__ == '__main__':
  23. # Example usage
  24. log_files = ["log1.txt", "log2.txt"] # Replace with your log file paths
  25. # Create dummy log files for testing
  26. with open("log1.txt", "w") as f:
  27. f.write("2023-10-27 10:00:00 - INFO - Starting process\n")
  28. f.write("2023-10-27 10:00:05 - DEBUG - Debugging information\n")
  29. with open("log2.txt", "w") as f:
  30. f.write("2023-10-27 10:00:10 - WARNING - Potential issue\n")
  31. f.write("2023-10-27 10:00:15 - INFO - Process completed\n")
  32. stream_log_files(log_files, interval=5) # Stream logs every 5 seconds

Add your comment