Skip to content

Instantly share code, notes, and snippets.

@JeOam
Last active March 13, 2023 12:13
Show Gist options
  • Save JeOam/0c7d55a493a652c8f41a to your computer and use it in GitHub Desktop.
Save JeOam/0c7d55a493a652c8f41a to your computer and use it in GitHub Desktop.
Extract error log information from logfle
#!/usr/bin/env python
# coding: utf-8
'''
Extract error log information from logfle
run:
$ python exlog.py file.log
Please enter an integer, last n (n > 1) day you want to extract:
Exactract finished.
and see the result: E_file.log
Log Template:
[INFO 2015-09-06 15:46:06,206 ....
[WARNING 2015-09-06 15:05:08,525 .....
[ERROR 2015-09-06 13:56:44,398 .......
Traceback (most recent call last):
File ".....
TypeError: '......
'''
import sys
import re
from datetime import datetime, timedelta
def parse_log_file(file_name, result_file, start_date):
enter_error_line = False
with open(file_name, 'r') as f:
for line in f:
if re.match('\[ERROR', line) != None or re.match('\[WARNING', line):
line_date_re = re.search(r'\b\d{4}[-]\d{2}[-]\d{2}\b', line)
if line_date_re:
line_date = datetime.strptime(line_date_re.group(), '%Y-%m-%d')
line_date = line_date.date()
if line_date >= start_date:
if enter_error_line is True:
result_file.write('\n')
else:
enter_error_line = True
result_file.write(line)
else:
if enter_error_line is True:
result_file.write('\n')
else:
enter_error_line = True
result_file.write(line)
elif enter_error_line == True and re.match('\[INFO', line) == None:
result_file.write(line)
elif re.match('\[INFO', line) != None:
if enter_error_line == True:
result_file.write('\n')
enter_error_line = False
if __name__ == "__main__":
file_path = sys.argv[1]
n = int(raw_input("Please enter an integer, last n (n > 1) day you want to extract: "))
start_date = datetime.now().date() - timedelta(days=n)
print 'Try to exatract error logs from:'
print file_path
result_file = open('E_'+file_path, 'w')
parse_log_file(file_path, result_file, start_date)
parse_log_file(file_path+'.1', result_file, start_date)
result_file.close()
print 'Exactract finished.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment