import os
import datetime
import json
def archive_log_entries(log_file, archive_dir):
"""
Archives log entries from a specified log file to a directory.
"""
# Ensure the archive directory exists
if not os.path.exists(archive_dir):
os.makedirs(archive_dir)
try:
with open(log_file, 'r') as f:
log_entries = f.readlines() # Read all lines into a list
except FileNotFoundError:
print(f"Error: Log file '{log_file}' not found.")
return
now = datetime.datetime.now()
archive_filename = f"log_{now.strftime('%Y%m%d_%H%M%S')}.json"
archive_path = os.path.join(archive_dir, archive_filename)
try:
# Create a list of dictionaries to store log entries
archived_data = []
for entry in log_entries:
try:
# Attempt to parse each log line as JSON
log_data = json.loads(entry.strip())
archived_data.append(log_data)
except json.JSONDecodeError:
print(f"Warning: Could not parse log entry: {entry.strip()}")
# Write the archived data to a JSON file
with open(archive_path, 'w') as outfile:
json.dump(archived_data, outfile, indent=4) #indent for readability
print(f"Log entries archived to: {archive_path}")
except Exception as e:
print(f"An error occurred during archiving: {e}")
if __name__ == '__main__':
# Example usage:
log_file = 'short_task.log' # Replace with your log file
archive_dir = 'archives' # Replace with your desired archive directory
# Create a dummy log file for testing
with open(log_file, 'w') as f:
f.write('{"timestamp": "2023-10-27T10:00:00", "message": "Task started"}\n')
f.write('{"timestamp": "2023-10-27T10:00:05", "message": "Task completed"}\n')
f.write('Invalid JSON log entry\n') # Test invalid JSON
f.write('{"timestamp": "2023-10-27T10:00:10", "message": "Another task started"}\n')
archive_log_entries(log_file, archive_dir)
Add your comment