Editor

Generate Error Summary

Advanced · Write the code · from Build a Log File Parser That Finds Errors and Patterns

From the lesson: Step 5: Generate an Error Summary

Before we build the full parser class, let's create a summary function that gives a quick overview of what happened in the log. This is the kind of output an on-call engineer wants to see: how many total entries, how many errors, what the most common error type is, and the worst burst.

Read this part of the lesson

Write a function error_summary(log_text) that prints:

1. Total log entries

2. Count of each severity level

3. The most frequent error message (the exact message that appears most often)

4. The hour with the most errors (format: HH:00)

Format:

=== ERROR SUMMARY ===
Total entries: X
INFO: X | WARNING: X | ERROR: X | CRITICAL: X
Most frequent error: MESSAGE (X occurrences)
Peak error hour: HH:00
import re
from collections import Counter
def parse_log_line(line):
pattern = r'(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}) (\w+) (.+)'
match = re.match(pattern, line)
if not match:
return None
date, time, level, message = match.groups()
return {'date': date, 'time': time, 'level': level, 'message': message}
def error_summary(log_text):
# Your code here
pass
log_text = """2024-03-15 08:23:01 INFO Server started
2024-03-15 08:25:12 WARNING High memory usage
2024-03-15 08:30:45 ERROR Connection refused
2024-03-15 08:30:46 ERROR Connection refused
2024-03-15 08:30:48 ERROR Retry failed
2024-03-15 09:45:00 ERROR Database timeout
2024-03-15 10:00:01 INFO Backup started
2024-03-15 10:05:22 ERROR Backup failed
2024-03-15 10:05:23 CRITICAL Disk space critical
2024-03-15 12:00:00 INFO Health check passed"""
error_summary(log_text)

More Log Parser exercises

All Python exercises · 50 practice problems · Practical Projects quiz