Lesson 1 of 0
In Progress

Parsing Log Entries

Kedeisha June 16, 2023

Write a Python function that parses a log entry and extracts the timestamp, log level, and message. Assume log entries are in the following format: “[YYYY-MM-DD HH:MM:SS] LEVEL – Message”

log_entry = ‘[2023-06-16 12:34:56] INFO – This is a log message.’ 

Explanation of Solution: The function parse_log accepts a single string as input. It defines a pattern that matches a string representing a log entry and divides it into three groups: timestamp, log level, and message. re.match function is used to check if the input string matches this pattern. If the match is found, the function returns a dictionary with the matched groups; otherwise, it returns None.