DEV Community

Cover image for Day 10: Log Analyzer and Report Generator
Udoh Deborah
Udoh Deborah

Posted on

Day 10: Log Analyzer and Report Generator

At the beginning of the challenge, the first thing we were asked to do was to Fork the repository, but I will do a step by step on how to clone the repository to submit Day 10 task using your forked repo.

Step 1: Clone Your Fork (if not already done)

If you haven’t cloned your fork to your local machine yet:

git clone https://github.com/<your-username>/90DaysOfDevOps.git
cd 90DaysOfDevOps
Enter fullscreen mode Exit fullscreen mode

Replace <your-username> with your GitHub handle (e.g., debsinthecloud).

Step 2: Create a Branch for Day 10

git checkout -b day10-log-analyzer
Enter fullscreen mode Exit fullscreen mode

Step 3: Create the Script File

Navigate to the folder where Day 10 content belongs (create it if you haven't created before)

mkdir -p Day10
cd Day10
Enter fullscreen mode Exit fullscreen mode

Now create your script:

nano log_analyzer.sh
Enter fullscreen mode Exit fullscreen mode

Paste the following:

#!/bin/bash

# Check if file path is provided
if [ -z "$1" ]; then
  echo "❌ Usage: $0 <log_file>"
  exit 1
fi

log_file="$1"

if [ ! -f "$log_file" ]; then
  echo "❌ File not found: $log_file"
  exit 1
fi

# Variables
report="log_summary_$(date '+%Y-%m-%d').txt"
total_lines=$(wc -l < "$log_file")
error_count=$(grep -E "ERROR|Failed" "$log_file" | wc -l)
top_errors=$(grep -E "ERROR|Failed" "$log_file" | sort | uniq -c | sort -nr | head -5)
critical=$(grep -n "CRITICAL" "$log_file")
log_name=$(basename "$log_file")

# Write report
{
  echo "📝 Log Analysis Report"
  echo "Date: $(date)"
  echo "Log File: $log_name"
  echo "Total Lines: $total_lines"
  echo "Total Errors: $error_count"
  echo
  echo "🔥 Top 5 Error Messages:"
  echo "$top_errors"
  echo
  echo "🚨 Critical Events (with line numbers):"
  echo "$critical"
} > "$report"

echo "✅ Report generated: $report"
Enter fullscreen mode Exit fullscreen mode

Save and exit.

Step 4: Make Script Executable

chmod +x log_analyzer.sh
Enter fullscreen mode Exit fullscreen mode

Step 5: Add and Commit Your Work

bash
git add Day10/log_analyzer.sh
Enter fullscreen mode Exit fullscreen mode
git commit -m "Day 10: Log Analyzer and Report Generator Script"
Enter fullscreen mode Exit fullscreen mode

Step 6: Push to Your Fork

git push origin day10-log-analyzer
Enter fullscreen mode Exit fullscreen mode

Step 7: Create Pull Request

  1. Go to your GitHub fork in the browser.
  2. You’ll see a prompt: “Compare & pull request” — click it.
  3. Add a title like: Day 10 - Log Analyzer Script
  4. Add a short message about your implementation.
  5. Click Create Pull Request.

our goal for day 10 is essentially to show

This script automates the process of analyzing log files by:

  • Counting total errors (using keywords like ERROR and Failed)

  • Extracting and listing all critical events with line numbers

  • Displaying the top 5 most frequent error messages

  • Generating a timestamped summary report with all details

It also includes error handling for invalid file paths. Tested with sample_log.log.

Top comments (0)