1. import os
  2. import datetime
  3. import json
  4. def archive_log_entries(log_file, archive_dir):
  5. """
  6. Archives log entries from a specified log file to a directory.
  7. """
  8. # Ensure the archive directory exists
  9. if not os.path.exists(archive_dir):
  10. os.makedirs(archive_dir)
  11. try:
  12. with open(log_file, 'r') as f:
  13. log_entries = f.readlines() # Read all lines into a list
  14. except FileNotFoundError:
  15. print(f"Error: Log file '{log_file}' not found.")
  16. return
  17. now = datetime.datetime.now()
  18. archive_filename = f"log_{now.strftime('%Y%m%d_%H%M%S')}.json"
  19. archive_path = os.path.join(archive_dir, archive_filename)
  20. try:
  21. # Create a list of dictionaries to store log entries
  22. archived_data = []
  23. for entry in log_entries:
  24. try:
  25. # Attempt to parse each log line as JSON
  26. log_data = json.loads(entry.strip())
  27. archived_data.append(log_data)
  28. except json.JSONDecodeError:
  29. print(f"Warning: Could not parse log entry: {entry.strip()}")
  30. # Write the archived data to a JSON file
  31. with open(archive_path, 'w') as outfile:
  32. json.dump(archived_data, outfile, indent=4) #indent for readability
  33. print(f"Log entries archived to: {archive_path}")
  34. except Exception as e:
  35. print(f"An error occurred during archiving: {e}")
  36. if __name__ == '__main__':
  37. # Example usage:
  38. log_file = 'short_task.log' # Replace with your log file
  39. archive_dir = 'archives' # Replace with your desired archive directory
  40. # Create a dummy log file for testing
  41. with open(log_file, 'w') as f:
  42. f.write('{"timestamp": "2023-10-27T10:00:00", "message": "Task started"}\n')
  43. f.write('{"timestamp": "2023-10-27T10:00:05", "message": "Task completed"}\n')
  44. f.write('Invalid JSON log entry\n') # Test invalid JSON
  45. f.write('{"timestamp": "2023-10-27T10:00:10", "message": "Another task started"}\n')
  46. archive_log_entries(log_file, archive_dir)

Add your comment