1. import re
  2. def clean_log_data(log_file_path, override_file_path=None):
  3. """
  4. Cleans log data from a file, allowing for manual overrides.
  5. Args:
  6. log_file_path (str): Path to the log file.
  7. override_file_path (str, optional): Path to a file containing overrides
  8. (key: value pairs for replacement).
  9. Defaults to None.
  10. Returns:
  11. list: A list of cleaned log lines.
  12. """
  13. cleaned_lines = []
  14. overrides = {}
  15. # Load overrides from file if provided
  16. if override_file_path:
  17. try:
  18. with open(override_file_path, 'r') as f:
  19. for line in f:
  20. key, value = line.strip().split('=', 1)
  21. overrides[key.strip()] = value.strip()
  22. except FileNotFoundError:
  23. print(f"Warning: Override file not found: {override_file_path}")
  24. except ValueError:
  25. print(f"Warning: Invalid format in override file: {override_file_path}")
  26. try:
  27. with open(log_file_path, 'r') as f:
  28. for line in f:
  29. cleaned_line = line.strip()
  30. # 1. Remove timestamps (example: remove anything before the first space)
  31. cleaned_line = cleaned_line.split(' ', 1)[1] if ' ' in cleaned_line else cleaned_line
  32. # 2. Replace specific patterns (example: replace 'ERROR' with 'WARNING')
  33. cleaned_line = re.sub(r'ERROR', 'WARNING', cleaned_line)
  34. cleaned_line = re.sub(r'DEBUG', 'INFO', cleaned_line)
  35. # 3. Apply overrides
  36. for key, value in overrides.items():
  37. cleaned_line = cleaned_line.replace(key, value)
  38. cleaned_lines.append(cleaned_line)
  39. except FileNotFoundError:
  40. print(f"Error: Log file not found: {log_file_path}")
  41. return []
  42. except Exception as e:
  43. print(f"An error occurred: {e}")
  44. return []
  45. return cleaned_lines
  46. if __name__ == '__main__':
  47. # Example usage:
  48. log_file = 'sample.log'
  49. override_file = 'overrides.txt'
  50. # Create a sample log file
  51. with open(log_file, 'w') as f:
  52. f.write("2023-10-26 10:00:00 INFO This is a log message.\n")
  53. f.write("2023-10-26 10:00:05 ERROR Something went wrong.\n")
  54. f.write("2023-10-26 10:00:10 DEBUG This is a debug message.\n")
  55. f.write("Another log message with ERROR.\n")
  56. f.write("Some other text.\n")
  57. # Create a sample override file
  58. with open(override_file, 'w') as f:
  59. f.write("ERROR=CRITICAL\n")
  60. f.write("DEBUG=VERBOSE\n")
  61. cleaned_data = clean_log_data(log_file, override_file)
  62. for line in cleaned_data:
  63. print(line)

Add your comment