Skip to content

35. Parse Log Lines into Records

Difficulty: Easy · Topic: Data wrangling

Application logs look like 2026-01-05T10:00:01 ERROR [api] timeout after 30s: a timestamp, a level, a service in square brackets, then the message. Write parse_logs(lines) returning a list of dicts with keys ts, level, service and message.

Your code runs in CPython on our servers against 4 tests, including hidden ones.

Sample test

parse_logs(["2026-01-05T10:00:01 ERROR [api] timeout after 30s", "garbage", "2026-01-05T10:00:02 info [db] connected"])

Expected: [{'ts': '2026-01-05T10:00:01', 'level': 'ERROR', 'service': 'api', 'message': 'timeout after 30s'}, {'ts': '2026-01-05T10:00:02', 'level': 'INFO', 'service': 'db', 'message': 'connected'}]

Hints

Hint 1One regular expression with named groups reads all four fields: (?P<ts>\S+) and so on.
Hint 2Match the level with the re.IGNORECASE flag, then call .upper().
Hint 3m.groupdict() turns a match into a dict.

Learn the concepts

More Python problems

All Python problems