Created
April 8, 2020 13:57
-
-
Save josegomezr/119edea66018bd57c77093d754849235 to your computer and use it in GitHub Desktop.
Python Structured logging aggregator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
peewee==3.13.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment