Skip to content

Instantly share code, notes, and snippets.

@1328
Created January 5, 2016 16:53
Show Gist options
  • Select an option

  • Save 1328/c4bbd3de013ef5aaad94 to your computer and use it in GitHub Desktop.

Select an option

Save 1328/c4bbd3de013ef5aaad94 to your computer and use it in GitHub Desktop.
a.py
LOG = '''
Tue Feb 24 00:00:02 2015 [11272386][-none-][FATAL] IBMDB2Manager: Persistent connection specified, but failed. Error: 42968: [IBM][CLI Driver] SQL8001N An attempt to connect to the database failed due to a product license problem. SQLSTATE=42968 SQLCODE=-8001
Tue Feb 24 00:00:02 2015 [12517544][-none-][FATAL] IBMDB2Manager: Persistent connection specified, but failed. Error: 42968: [IBM][CLI Driver] SQL8001N An attempt to connect to the database failed due to a product license problem. SQLSTATE=42968 SQLCODE=-8001
Tue Feb 24 00:00:02 2015 [11272386][-none-][FATAL] IBMDB2Manager: Could not connect to Database with non-persistent connection. Error 42968: [IBM][CLI Driver] SQL8001N An attempt to connect to the database failed due to a product license problem. SQLSTATE=42968 SQLCODE=-8001
Tue Feb 24 00:00:02 2015 [11272386][-none-][FATAL] Could Not Connect:: Database Is Not Connected
Tue Feb 24 00:00:02 2015 [11272386][-none-][FATAL] Stack trace for DB_ERROR:
#0 custom/include/database/CustomIBMDB2Manager.php(631): CustomDBLogger->trace('DB_ERROR')
#1 include/database/DBManager.php(334): CustomIBMDB2Manager->registerError('Could Not Conne...', 'Database Is Not...', true)
Tue Feb 25 00:00:02 2015 [11272386][-none-][FATAL] Stack trace for db2_prepare() failed with this sql:
SELECT category, name, value, platform FROM config WHERE category = 'tracker':
#0 custom/include/database/CustomIBMDB2Manager.php(503): CustomDBLogger->trace('db2_prepare() f...')
#14 {main}
Tue Feb 26 00:00:02 2015 [11272386][-none-][FATAL] Unable to retrieve system settings Query Failed: SELECT category, name, value, platform FROM config WHERE category = 'tracker': Database Is Not Connected
Tue Feb 26 00:00:02 2015 [11272386][-none-][FATAL] Stack trace for DB_ERROR:
#0 custom/include/database/CustomIBMDB2Manager.php(631): CustomDBLogger->trace('DB_ERROR')
'''.strip().split('\n')
import datetime
from collections import namedtuple
Record = namedtuple('record', ['date', 'error', 'trace'])
# note: I am using a bit of shenanigans here to put mutable objects into a
# namedtuple. Normally I would create a class for this, but this works well too
# as a quick and dirty short-cut
# ideally you would have its own class with __repr__ defined to print pretty
def parse_time(line):
'''parse time out of a logfile line'''
try:
raw_time = line.split('[')[0].strip()
parse_format = '%a %b %d %H:%M:%S %Y'
time = datetime.datetime.strptime(raw_time, parse_format)
return time
except ValueError:
# not a date line
return None
def parse_log(log):
'''parse the log into Records'''
current = None
result = []
for line in log:
time = parse_time(line)
if time is not None:
# we have a line with time
if current is None:
# first record
# create new current object to store data
current = Record(time, [line],[])
elif current.trace:
# another record:
# already have a trace, meaning we have a new error
# store the current one
result.append(current)
# and put line and time into a new record
current = Record(time, [line],[])
else:
# have a time line after another time line
# consolidate
current.error.append(line)
else:
if current is None:
# found a trace line before finding a time line in file
raise Exception('bad line {}'.format(line))
else:
# add trace to current record
current.trace.append(line)
if current is not None:
result.append(current)
return result
def get_start():
'''use arge parse to get starttime/date. Here i just pick an arbitrary
one'''
return datetime.datetime(2015,2,25)
def filter_start(records, start):
'''filter a parsed log file, removing records before start'''
result = []
for record in records:
if record.date<start:
continue
result.append(record)
return result
def main():
start = get_start()
records = parse_log(LOG)
found = filter_start(records, start)
for record in found:
print(record)
print()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment