- This recipe will need three components:
import re from pathlib import Path import csv
- Here's the pattern that matches the simple Flask logs. For other kinds of logs, or other formats configured into Flask, a different pattern will be required:
log_pattern = re.compile( r"\[(?P<timestamp>.*?)\]" r"\s(?P<levelname>\w+)" r"\sin\s(?P<module>[\w\._]+):" r"\s(?P<message>.*)")
- Here's the function that yields dictionaries for the matching rows. This applies the regular expression pattern. Non-matches are silently skipped. The matches will yield a dictionary of item names and their values:
def extract_row_iter(source_log_file): for line in source_log_file: match = log_pattern.match(line) if match is None: continue yield ...