Skip to content

Instantly share code, notes, and snippets.

@wiljdaws
Last active August 14, 2023 19:16
Show Gist options
  • Save wiljdaws/241d82c0b1663d6028f7f94046840539 to your computer and use it in GitHub Desktop.
Save wiljdaws/241d82c0b1663d6028f7f94046840539 to your computer and use it in GitHub Desktop.
Log error and info seperator
import re
import os
import logging
def read_error_log():
'''
Read weekly_hr.log file and create two new files:
1. errors.log - contains all errors
2. info.log - contains all info
messages
are written to both log files
:return: None, writes the logs in current directory
'''
# get cwd
cwd = os.getcwd()
if not os.path.exists(f"{cwd}\main\log\weekly_hr.log"):
# create empty file path
open(f"{cwd}\main\log\weekly_hr.log", "w").close()
with open(f"{cwd}\main\log\weekly_hr.log", "r") as f:
log = f.read()
# regex to find errors in log file
errors = re.findall(r".*ERROR.*", log)
# regex to find info in log file
info = re.findall(r".*INFO.*", log)
# create a new file to log errors
with open(f"{cwd}\main\log\errors.log", "w") as f:
for error in errors:
f.write(error + "\n")
# create a new file to log info
with open(f"{cwd}\main\log\info.log", "w") as f:
for inf in info:
f.write(inf + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment