Automation scripts grow into tools, and tools need structure. This module
refactors the log analyzer into a class — the way a lot of internal DevOps tools
are built. Same behaviour as
03-file-handling-and-logs, now with state in
__init__ and behaviour in methods. Light, practical OOP — no inheritance or
abstract classes.
app.log— sample loglog_analyzer_oop.py— theLogAnalyzerclass
python log_analyzer_oop.pyExpected: INFO=10, WARNING=2, ERROR=3 for the bundled log.
Build the LogAnalyzer class yourself:
- In
__init__, store the log file path and set up acountsdict. read_logs()returns the file's lines (handle a missing file).analyze()countsINFO/WARNING/ERROR— checkLEVEL in tokens.- Create an object and print the summary.
- Bonus: add a
write_summary()method that dumpscountsto JSON.