Skip to content

Instantly share code, notes, and snippets.

@0atman
Created August 14, 2013 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0atman/6230261 to your computer and use it in GitHub Desktop.
Save 0atman/6230261 to your computer and use it in GitHub Desktop.
errors-by-date.py
import os
import datetime
from collections import Counter
import json
import argparse
parser = argparse.ArgumentParser(
description='Count Tomcat errors and group by date.'
)
parser.add_argument(
'path',
metavar='path',
type=str,
help='the path to the log files'
)
args = parser.parse_args()
dates = []
for root,dirs,files in os.walk(args.path):
print len(files), "files in", args.path
for file in files:
f=open(os.path.join(root,file), 'r')
for line in f.readlines():
if "ERROR" in line:
if "[" not in line.split(',')[0]:
date = line.split(',')[0]
else:
date = line.split(',')[0].split('[')[1]
day = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S').date()
dates.append(day.strftime('%Y-%m-%d'))
f.close()
date_counter = Counter(dates)
print json.dumps(date_counter, indent=2)
print reduce(lambda x, y: x + y, date_counter.values(), 0) , "errors"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment