EasyParsingPython 3
Log Error Parser
Summarize top error messages from noisy logs with deterministic tie-breaking.
20m3 sample tests7 hidden tests
Implement top_errors(log_text, limit=3) for application logs.
Requirements
- Input is one string containing newline-separated log lines.
- Count only lines that contain
" ERROR ". - The error key is the message after
" ERROR ". - Strip leading and trailing whitespace from the extracted message.
- Exclude empty messages, including whitespace-only messages after stripping.
- Ignore indented continuation lines for counting.
- Return a list of
(message, count)pairs. - Sort by count descending, then message ascending.
- Return at most
limitrows. - If
limit <= 0, return an empty list.
Example
python
1log_text = '''
22026-01-01 INFO start
32026-01-01 ERROR timeout
42026-01-01 ERROR bad gateway
52026-01-01 ERROR timeout
6'''
7
8assert top_errors(log_text, limit=2) == [("timeout", 2), ("bad gateway", 1)]Constraints
- Use standard-library Python only.
- Prefer
splitlines,Counter, or an explicit state machine.
Editor