Python Log Monitoring with Timestamp & Alerts for DevOps

🚀 Python for DevOps – Log Monitoring with Timestamp & Alerts (Mini Project) Built a hands-on Python script to analyze logs, generate alerts, and track system health — a small step toward real-world DevOps automation. 📂 Problem: Manually scanning logs is inefficient and error-prone. Needed a way to automatically filter and track critical issues. 💻 Solution (Python Script): from datetime import datetime ERROR_COUNT = 0 WARNING_COUNT = 0 INFO_COUNT = 0 with open("app.log") as f, open("alerts.log", "a") as alert_file: for line in f: timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") if "ERROR" in line: ERROR_COUNT += 1 alert_file.write(f"{timestamp} - {line.strip()}\n") elif "WARNING" in line: WARNING_COUNT += 1 alert_file.write(f"{timestamp} - {line.strip()}\n") elif "INFO" in line: INFO_COUNT += 1 print("============ LOG SUMMARY ============") print("ERROR:", ERROR_COUNT) print("WARNING:", WARNING_COUNT) print("INFO:", INFO_COUNT) Output: ubuntu@satheesha:~/python$ python3 log-mon_alert-time.py ============LOG SUMMARY================ ERROR: 1 WARNING: 1 INFO: 2 ubuntu@satheesha:~/python$ cat alerts.log 2026-04-21 11:37:1776771454 - INFO - INFO: Service startes 2026-04-21 11:37:1776771454 - WARNING - WARNING: High CPU 2026-04-21 11:37:1776771454 - INFO - INFO: Service startes 2026-04-21 11:37:1776771454 - ERROR - ERROR: Disk full 2026-04-21 11:45:59 - INFO - INFO: Service startes 2026-04-21 11:45:59 - WARNING - WARNING: High CPU 2026-04-21 11:45:59 - INFO - INFO: Service startes 2026-04-21 11:45:59 - ERROR - ERROR: Disk full 🔍 What this script does: Reads application logs (app.log) Filters critical log levels (ERROR / WARNING / INFO) Appends important alerts into alerts.log Adds timestamps for better traceability Generates summary metrics for quick insights 📊 Why this matters: Faster troubleshooting in production Clear visibility into system health Reduces manual effort in log analysis 🔥 Key Learning: Python is a powerful tool in DevOps—not just for scripting, but for automation, monitoring, and observability. 📈 Next Steps: Add alerting (Email / Slack integration) Convert logs to structured format (JSON for ELK stack) Build real-time log monitoring (tail -f style) #Python #DevOps #Automation #Logging #Monitoring #Cloud #Scripting #Learning #100DaysOfCode

To view or add a comment, sign in

Explore content categories