import re import sqlite3 REGEX = re.compile( r'^' r'{address space usage: (?P\d+) bytes/(?P\w+)} ' r'{rss usage: (?P\d+) bytes/(?P\w+)} ' r'\[pid: (?P\d+)\|app: -\|req: -/-\] (?P[0-9.]+) \(-\) ' r'{(?P\d+) vars in (?P\d+) bytes} ' r'\[(?P[0-9A-Za-z: ]+)\] (?P\w+) (?P.*?) => ' r'generated (?P\d+) bytes in (?P\d+) msecs ' r'\((?P[A-Z0-9/.]+) (?P\d+)\) ' r'(?P\d+) headers in (?P\d+) bytes ' r'\((?P\d+) switches on core (?P\d+)\) ' r'(?P.*?)' r'$' ) def save_logs(logs): columns = sorted(REGEX.groupindex, key=lambda col: REGEX.groupindex[col]) conn = sqlite3.Connection('logs.db') cur = conn.cursor() cur.execute('CREATE TABLE logs(%s)' % ', '.join(columns)) cur.executemany('INSERT INTO logs VALUES (%s)' % ', '.join(['?'] * len(columns)), [[log[col] for col in columns] for log in logs]) conn.commit() conn.close() def read_logs(path): with open(path) as fp: lines = fp.readlines() return [REGEX.match(line.strip()).groupdict() for line in lines if line.startswith('{address space usage')] if __name__ == '__main__': save_logs(read_logs('uwsgi.log'))