Parse logs and count error codes
Problem Description
You are given a list of application log lines. Each line is expected to adhere to the following format:
[LEVEL] timestamp, eventName, errorCode, text
LEVELis a single character (e.g.,I,W,E) enclosed in square brackets (e.g.,[I]).- Fields following the
LEVELare comma-separated. - The
textfield can contain arbitrary characters but is guaranteed not to contain a newline.
Task
Write a Python function that processes a list of these log lines and calculates the frequency of each unique errorCode encountered.
Input
A list of strings, lines, where each string represents a single log line.
Output
A dictionary (or map) where keys are errorCode strings and values are their respective occurrence counts (integers).
Requirements and Edge Cases
- Whitespace around comma-separated fields must be trimmed.
- Lines that do not conform to the specified format should be gracefully ignored without causing the program to crash.
errorCodevalues should be treated as strings (e.g.,"404","E123").
Example
Input:
[I] 2026-01-01T00:00:00Z, Login, E123, user not found
[W] 2026-01-01T00:00:01Z, Login, E123, retry
[E] 2026-01-01T00:00:02Z, Payment, P007, declined
Output:
{ "E123": 2, "P007": 1 }
AI interview prep
Core dilemma
Balancing robust and fault-tolerant parsing of semi-structured log data with efficient extraction and aggregation of specific fields, especially under varying input quality.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Parse logs and count error codes
Problem Description
You are given a list of application log lines. Each line is expected to adhere to the following format:
[LEVEL] timestamp, eventName, errorCode, text
LEVELis a single character (e.g.,I,W,E) enclosed in square brackets (e.g.,[I]).- Fields following the
LEVELare comma-separated. - The
textfield can contain arbitrary characters but is guaranteed not to contain a newline.
Task
Write a Python function that processes a list of these log lines and calculates the frequency of each unique errorCode encountered.
Input
A list of strings, lines, where each string represents a single log line.
Output
A dictionary (or map) where keys are errorCode strings and values are their respective occurrence counts (integers).
Requirements and Edge Cases
- Whitespace around comma-separated fields must be trimmed.
- Lines that do not conform to the specified format should be gracefully ignored without causing the program to crash.
errorCodevalues should be treated as strings (e.g.,"404","E123").
Example
Input:
[I] 2026-01-01T00:00:00Z, Login, E123, user not found
[W] 2026-01-01T00:00:01Z, Login, E123, retry
[E] 2026-01-01T00:00:02Z, Payment, P007, declined
Output:
{ "E123": 2, "P007": 1 }