Error log file searcher

PHOTO EMBED

Sat Mar 27 2021 14:45:04 GMT+0000 (UTC)

Saved by @FlorianC #python

#!/usr/bin/env python3
import sys
import os
import re

def error_search(log_file):
  error = input("What is the error")
  returned_errors = []

  with open(log_file, mode="r",encoding="UTF-8") as file:
    for log in file.readlines():
      error_patterns = ["error"]
      for i in range(len(error.split(" "))):
        error_patterns.append(r"{}".format(error.split(" ")[i].lower()))
      if all(re.search(error_pattern, log.lower()) for error_pattern in error_patterns):
        returned_errors.append(log)
    file.close()
  return returned_errors

def file_output(returned_errors):
  with open(os.path.expanduser("~") + "/data/errors_found.log", "w") as file:
    for error in returned_errors:
      file.write(error)
    file.close()

if __name__ == "__main__":
  log_file = sys.argv[1] # take the first parameter passed as the path of the log file
  returned_errors = error_search(log_file)
  file_output(returned_errors)
  sys.exit(0) # exits python and gives exit status of 0 here
content_copyCOPY

Script that can search for specific errors as indicated by the user in log files