Skip to content

Instantly share code, notes, and snippets.

@robrocker7
Created September 11, 2013 16:48
Show Gist options
  • Save robrocker7/6526441 to your computer and use it in GitHub Desktop.
Save robrocker7/6526441 to your computer and use it in GitHub Desktop.
Basic Apache Error Log Parser
import re
import os
import sys
import getopt
import datetime
import csv
class RobLog(object):
input_file = None
output_file = None
regex = '\[(.*?)\] \[(.*?)\] (.*)'
line_map = ('date', 'type', 'message')
json_output = []
def prepare_output(self):
print 'RobLog: ... preparing CSV output...'
f = open(self.output_file, 'wb')
writer = csv.writer(f, delimiter=',', quotechar='"')
writer.writerow(self.line_map)
for line in self.json_output:
writer.writerow([
line['date'].isoformat(),
line['type'],
line['message']
])
f.close()
print 'RobLog: ... CSV completed!'
def parse_line(self, line):
g = re.match(self.regex, line).groups()
formatted_line = dict(zip(self.line_map, g))
# get the date and turn it into a datetime
formatted_line['date'] = datetime.datetime.strptime(formatted_line['date'],
'%a %b %d %H:%M:%S %Y')
return formatted_line
def parse_file(self):
print 'RobLog: Parsing log file...'
f = open(self.input_file, 'rb')
for line in f:
f_line = self.parse_line(line)
self.json_output.append(f_line)
f.close()
print 'RobLog: ... parsed {0} lines...'.format(len(self.json_output))
self.prepare_output()
def __init__(self, argv):
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
self.input_file = arg
elif opt in ("-o", "--ofile"):
self.output_file = arg
self.parse_file()
if __name__ == "__main__":
RobLog(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment