The Complete LogParser Class
Advanced · Write the code · from Build a Log File Parser That Finds Errors and Patterns
From the lesson: Step 6: Build the Complete LogParser Class
Now let's wrap everything into a reusable class. A LogParser class gives us a clean interface: load the logs once, then call different analysis methods as needed. This is how professional log analysis tools are structured.
Read this part of the lessonBuild a LogParser class with:
`__init__(self, log_text)`: Parse all lines and store the entries as a list of dicts.
`level_counts(self)`: Return a dict of level -> count.
`errors_only(self)`: Return a list of only ERROR and CRITICAL entries.
`most_common_error(self)`: Return a tuple of (message, count) for the most frequent error.
`report(self)`: Print a formatted summary report showing total entries, level breakdown, error count, and most common error.
Test it with the provided log data.
More Log Parser exercises
- Parse Log LinesAdvanced
- Count Log LevelsAdvanced
- Classify Errors by TypeAdvanced
- Find Error PatternsAdvanced
- Generate Error SummaryAdvanced
All Python exercises · 50 practice problems · Practical Projects quiz