Classify Errors by Type
Advanced · Write the code · from Build a Log File Parser That Finds Errors and Patterns
From the lesson: Step 3: Count Errors by Type
Knowing you have 5 errors is useful. Knowing that 3 of them are "connection refused" and 2 are "timeout" is much more useful. Let's classify error messages into types by extracting key phrases.
Read this part of the lessonWrite a function classify_errors(log_text) that:
1. Parses the log and filters for ERROR and CRITICAL entries only
2. Classifies each error into a type based on keywords in the message:
- Connection: message contains "connect" or "connection"
- Timeout: message contains "timeout" or "timed out"
- Authentication: message contains "auth" or "login"
- Disk: message contains "disk" or "space"
- Other: anything that doesn't match
3. Returns a dictionary mapping error types to their count
Print the error types sorted by count.
More Log Parser exercises
- Parse Log LinesAdvanced
- Count Log LevelsAdvanced
- Find Error PatternsAdvanced
- Generate Error SummaryAdvanced
- The Complete LogParser ClassAdvanced
All Python exercises · 50 practice problems · Practical Projects quiz