AW Dev Rethought

🌟 The best way to predict the future is to invent it - Alan Kay

🧩 Python Automation Recipes – 📄 Log Analyzer


Description:

📌 Introduction

Logs are one of the most useful sources of system truth — but reading them line by line is slow and inefficient.

A simple analyzer can help you quickly understand how many errors occurred, what types of events are most common, and how active the system has been.

This automation recipe shows how to parse log files and generate error/usage summaries using Python. It’s useful for debugging, monitoring, and operational reporting.


🔎 Explanation

  • The script reads a log file line by line.
  • It counts the occurrences of common log levels such as:
    • INFO
    • WARNING
    • ERROR
    • CRITICAL
  • It also keeps a total line count and extracts error lines into a separate list for review.
  • At the end, it prints a summary and saves a small report file.

This pattern is useful for:

  • application logs
  • server logs
  • batch jobs
  • automation workflows

✅ Key Takeaways

  • 📄 Parse logs automatically instead of reading manually.
  • 🚨 Summarize error and usage patterns quickly.
  • ⚙️ Great for debugging, reporting, and monitoring workflows.

Code Snippet:

from pathlib import Path
from collections import Counter

# --- Step 1: Configuration ---

# Path to the log file you want to analyze
LOG_FILE = Path("app.log")  # Change this

# Output file for the generated summary
REPORT_FILE = Path("log_summary_report.txt")

# --- Step 2: Validate input file ---
if not LOG_FILE.exists():
    raise FileNotFoundError(f"Log file not found: {LOG_FILE}")

# --- Step 3: Initialize counters and storage ---
log_levels = Counter()
total_lines = 0
error_lines = []

# --- Step 4: Read and analyze the log file ---
with LOG_FILE.open("r", encoding="utf-8") as f:
    for line in f:
        total_lines += 1
        line = line.strip()

        # Count common log levels
        if "INFO" in line:
            log_levels["INFO"] += 1
        if "WARNING" in line:
            log_levels["WARNING"] += 1
        if "ERROR" in line:
            log_levels["ERROR"] += 1
            error_lines.append(line)
        if "CRITICAL" in line:
            log_levels["CRITICAL"] += 1
            error_lines.append(line)

# --- Step 5: Build summary report ---
report = []
report.append("Log Analysis Report")
report.append("----------------------")
report.append(f"Total lines processed: {total_lines}")
report.append(f"INFO entries        : {log_levels['INFO']}")
report.append(f"WARNING entries     : {log_levels['WARNING']}")
report.append(f"ERROR entries       : {log_levels['ERROR']}")
report.append(f"CRITICAL entries    : {log_levels['CRITICAL']}")
report.append("")
report.append("Error / Critical Entries:")
report.append("---------------------------")

if error_lines:
    report.extend(error_lines[:10])  # show first 10 error lines
else:
    report.append("No ERROR or CRITICAL entries found.")

report_text = "\n".join(report)

# --- Step 6: Print and save report ---
print(report_text)

with REPORT_FILE.open("w", encoding="utf-8") as f:
    f.write(report_text)

print(f"\nSummary report saved to: {REPORT_FILE}")

Link copied!

Comments

Add Your Comment

Comment Added!