A service writes plain-text logs that are too large to load at once. Write a generator that reads them one line at a time, keeps the well-formed lines at or above a severity, and yields each as a structured record.
Implement parse_logs(lines, min_level="INFO", keyword=None) as a generator function.
Inputs
lines: any iterable of strings (a list, an open file, another generator). Lines may end in "\n" or "\r\n".min_level: one of DEBUG < INFO < WARNING < ERROR < CRITICAL.keyword: None, or a string that must appear in the message, compared case-insensitively.Line format (after stripping surrounding whitespace):
YYYY-MM-DD HH:MM:SS [LEVEL] component: message
LEVEL is one of the five names above, uppercase.component is one or more letters, digits, _, . or -.message is everything after ": ", stripped, and must be non-empty.2024-02-30 10:00:00 is invalid).Output: yield, in input order, one dict per valid line whose level is at least min_level and whose message matches keyword:
{"ts": "YYYY-MM-DDTHH:MM:SS", "level": LEVEL, "component": component, "message": message}. Invalid lines are skipped silently. The generator must be lazy: it reads a line only when the caller asks for the next record.