1. import re
  2. from datetime import datetime
  3. def sort_log_records(log_records, hypothesis_fields):
  4. """
  5. Sorts log records based on specified hypothesis fields,
  6. using fallback logic for missing or invalid fields.
  7. Args:
  8. log_records: A list of strings, where each string is a log record.
  9. hypothesis_fields: A list of strings representing the fields to sort by,
  10. in order of priority.
  11. Returns:
  12. A sorted list of log records. Returns the original list if an error occurs.
  13. """
  14. def parse_log_record(record):
  15. """Parses a log record and extracts field values."""
  16. try:
  17. # Regular expression to extract fields (adapt to your log format)
  18. match = re.match(r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}) - (.*) - (.*) - (.*)", record)
  19. if not match:
  20. return None #Or raise an exception, depending on desired behavior
  21. timestamp_str, field1, field2, field3 = match.groups()
  22. return {
  23. "timestamp": datetime.strptime(timestamp_str, "%Y-%m-%d %H:%M:%S,%f"),
  24. "field1": field1.strip(),
  25. "field2": field2.strip(),
  26. "field3": field3.strip()
  27. }
  28. except (ValueError, TypeError) as e:
  29. print(f"Error parsing record: {record}. Error: {e}")
  30. return None # Or raise
  31. def sort_key(record):
  32. """Generates a sort key based on hypothesis fields, with fallback."""
  33. if record is None: # Handle records that couldn't be parsed
  34. return (datetime.min, "", "") #Place invalid records at the beginning
  35. sort_values = []
  36. for field in hypothesis_fields:
  37. try:
  38. sort_values.append(getattr(record, field)) #Access record attributes
  39. except AttributeError:
  40. sort_values.append("") #Fallback to empty string if field is missing
  41. except Exception as e:
  42. print(f"Error accessing field {field}: {e}")
  43. sort_values.append("") #Fallback to empty string
  44. return tuple(sort_values) #Convert to tuple for consistent comparison
  45. parsed_records = [parse_log_record(record) for record in log_records]
  46. #Filter out any records that failed parsing
  47. valid_records = [r for r in parsed_records if r is not None]
  48. if not valid_records:
  49. print("No valid records found after parsing. Returning original list.")
  50. return log_records
  51. sorted_records = sorted(valid_records, key=sort_key)
  52. return sorted_records

Add your comment