import re
from datetime import datetime
def sort_log_records(log_records, hypothesis_fields):
"""
Sorts log records based on specified hypothesis fields,
using fallback logic for missing or invalid fields.
Args:
log_records: A list of strings, where each string is a log record.
hypothesis_fields: A list of strings representing the fields to sort by,
in order of priority.
Returns:
A sorted list of log records. Returns the original list if an error occurs.
"""
def parse_log_record(record):
"""Parses a log record and extracts field values."""
try:
# Regular expression to extract fields (adapt to your log format)
match = re.match(r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}) - (.*) - (.*) - (.*)", record)
if not match:
return None #Or raise an exception, depending on desired behavior
timestamp_str, field1, field2, field3 = match.groups()
return {
"timestamp": datetime.strptime(timestamp_str, "%Y-%m-%d %H:%M:%S,%f"),
"field1": field1.strip(),
"field2": field2.strip(),
"field3": field3.strip()
}
except (ValueError, TypeError) as e:
print(f"Error parsing record: {record}. Error: {e}")
return None # Or raise
def sort_key(record):
"""Generates a sort key based on hypothesis fields, with fallback."""
if record is None: # Handle records that couldn't be parsed
return (datetime.min, "", "") #Place invalid records at the beginning
sort_values = []
for field in hypothesis_fields:
try:
sort_values.append(getattr(record, field)) #Access record attributes
except AttributeError:
sort_values.append("") #Fallback to empty string if field is missing
except Exception as e:
print(f"Error accessing field {field}: {e}")
sort_values.append("") #Fallback to empty string
return tuple(sort_values) #Convert to tuple for consistent comparison
parsed_records = [parse_log_record(record) for record in log_records]
#Filter out any records that failed parsing
valid_records = [r for r in parsed_records if r is not None]
if not valid_records:
print("No valid records found after parsing. Returning original list.")
return log_records
sorted_records = sorted(valid_records, key=sort_key)
return sorted_records
Add your comment