Parse a log line

Mediumsplitmaxsplitdictionaries· 20 XP

Server logs are plain text, one event per line, like 2026-03-01 12:00:05 ERROR payment failed for order 88. Before counting errors, each line needs to be split into its parts.

Write a function parse_line(line) that returns a dictionary with the keys "date", "time", "level" and "message". The message is everything after the level, and it can contain spaces.

How it's checked

Your code runs, then the check below runs and its printed output is compared with ours.

  • parse_line("2026-03-01 12:00:05 ERROR payment failed") returns {"date": "2026-03-01", "time": "12:00:05", "level": "ERROR", "message": "payment failed"}
def parse_line(line):
    ...