Parse a Log Line Without Regex

Hard ⏱ 18 min 39% acceptance ★★★★★ 4.9
Server logs look like "2024-01-05 10:23:11 ERROR Payment failed for order 1004" — a date, a time, a severity level, then a free-text message. Write parse_log_line(line) that splits this into a dictionary with keys date, time, level, and message, using only str.split() (with a limited split count so the message itself can contain spaces).

Examples

Example 1
Input
line = '2024-01-05 10:23:11 ERROR Payment failed for order 1004'
Output
{'date': '2024-01-05', 'time': '10:23:11', 'level': 'ERROR', 'message': 'Payment failed for order 1004'}
Explanation

The first three space-separated tokens are date/time/level; everything after that is the message, kept intact.

Constraints

  • line always has at least a date, time, level and a non-empty message.

Topics

StringsParsing

Companies

AmazonIBMCisco

Hints

Hint 1

`line.split(' ', 3)` splits into at most 4 pieces, so the message keeps its internal spaces.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.