KikoffLog Parser with Query Operators
Part 1: parse List[str] logs into {timestamp, level, service, message, user_id}, dropping malformed lines. Part 2: search with leading type:value operators (level, service, userid, inclusive start, exclusive end) plus a trailing message keyword, returning the matches and their count.
Problem
Build a tool that parses and queries a set of server logs. Input is a list of log strings (List[str]) coming from different services.
Part 1: Parse and Filter
Implement a function that reads the logs and parses each into a structured object:
{
"timestamp": datetime or str,
"level": "INFO" | "WARN" | "ERROR" | "DEBUG",
"service": str,
"message": str,
"user_id": str or None # filled if a user_id can be extracted from message, else None
}- Raw line format:
YYYY-MM-DDTHH:MM:SS LEVEL SERVICE MESSAGE.LEVELis one of INFO / WARN / ERROR / DEBUG,SERVICEcontains no whitespace, andMESSAGEis everything after it. user_idis the first id inMESSAGEwritten asuser_id=<id>oruser_id:<id>, where<id>consists of letters, digits, underscore or hyphen; otherwiseNone.- Input lines may be malformed or not follow the format; drop any line with a missing field, an invalid timestamp, or an invalid level.
- Return
List[ParsedLog]containing every successfully parsed log.
Part 2: Search Query with Operators
Implement a search function over the parsed logs (up to 10^5 lines). The query string may begin with any number of type:value operators; all operators sit at the start of the query.
level:info/level:warn/level:error/level:debug— filter by level.service:<name>(e.g.service:auth,service:payment,service:user) — filter by service.userid:<id>(e.g.userid:12345) — only logs belonging to that user.start:<date>(e.g.start:2024-01-01) —timestamp >= date 00:00:00(inclusive).end:<date>(e.g.end:2024-01-31) —timestamp < date 00:00:00(exclusive).- Operators are combined with AND.
levelvalues are case-insensitive;serviceanduseridare exact matches. - Any text remaining after the operators is a case-insensitive substring search against
message.
Return all matching logs in input order and the match count. The exact shape is your choice, e.g. {"count": 12, "logs": [...]} or (List[ParsedLog], count).
Examples
level:error service:auth timeout—level = ERROR,service = auth, andmessagecontains"timeout".level:error service:auth— all ERROR logs from the auth service.level:info payment processed—level = INFOandmessagecontainspayment processed.userid:12345— all logs produced by that user.start:2024-01-01 end:2024-02-01—2024-01-01 <= timestamp < 2024-02-01.
Pitfalls
- The raw log-line layout and the way a
user_idappears insidemessageare sometimes not spelled out in the prompt — confirm both with the interviewer before writing the parser. - A line with an unknown level, an invalid timestamp or fewer than four fields must be dropped, not parsed with defaults.
- Only the first
user_idin a message counts; a second one later in the message is ignored. - Operator values are lowercase (
level:error) while parsed levels are uppercase (ERROR); normalize case before comparing. startis inclusive andendis exclusive. Off-by-one on the date bounds is the easiest bug to ship.- Comparing dates as strings only works if every timestamp is in the same sortable format; if you keep
timestampas a string, say so explicitly. - Parsing stops consuming operators at the first token that is not
type:value; everything after it, including multi-word text likepayment processed, is the keyword. - The function must return the count as well as the list.
Preparation
- In 25 minutes, implement Part 1 with a single regex or
splitper line, and test it on a malformed line, a line with an unknown level, and a line with no user id. - Implement Part 2 as a parse-query step (operators dict + remaining keyword) followed by one filter pass; test each of the five example queries above.
- Practice explaining how you would add a new operator type without touching the filter loop (e.g. a table of operator → predicate).
