Skip to content

Instantly share code, notes, and snippets.

@josegomezr
Created April 8, 2020 13:57
Show Gist options
  • Save josegomezr/119edea66018bd57c77093d754849235 to your computer and use it in GitHub Desktop.
Save josegomezr/119edea66018bd57c77093d754849235 to your computer and use it in GitHub Desktop.
Python Structured logging aggregator

A very dummy parser for structured logging format into SQLite db.

import glob
import os
import peewee
def chunks(lst, n):
"""Yield successive n-or-less-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
DB = peewee.SqliteDatabase('all_logs.db')
class LogLine(peewee.Model):
# Add/remove records as needed
date = peewee.DateTimeField()
controller = peewee.CharField(max_length=48)
action = peewee.CharField(max_length=48)
status = peewee.IntegerField()
duration = peewee.IntegerField()
view = peewee.IntegerField()
db = peewee.IntegerField()
guid = peewee.CharField(null=True)
class Meta:
database = DB
DB.connect()
DB.create_tables([LogLine])
counter = 0
print("START - Dumping")
for logfile in glob.glob('*/*/*.log'):
row_counter = 0
records = []
print(" > START - Processing {}".format(logfile))
for line in open(logfile):
record = dict(map(lambda x: x.split('='), line.split()))
record.setdefault('guid')
records.append(record)
counter += 1
row_counter += 1
print(" > END - Processing {}".format(logfile))
print(" > START - Saving to DB")
with DB.atomic() as txn:
pages = chunks(records, 100)
for key, page in enumerate(pages):
print("\r > Flushing {} page to DB".format(key+1), end="")
LogLine.insert_many(page).execute()
print("\n")
print("\r > Renaming logfile {}".format(logfile))
os.rename(logfile, logfile + '.done')
print(" > END - Saving to DB")
print("END - Dumping")
print("Saved {} records".format(counter))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment