Log File Analysis with Python for DevOps

🚀 Learning Python for DevOps – Hands-on Practice with Code Today I worked on a practical DevOps task: Log File Analysis using Python Started with a sample app.log: ERROR: Disk full WARNING: High CPU usage INFO: Service started INFO: Health check passed 🔍 Step 1: Read log file with open("app.log", "r") as f: data = f.read() print(data) ⚠️ Step 2: Filter only ERROR logs with open("app.log", "r") as f: for line in f: if "ERROR" in line: print(line) 📊 Step 3: Count total errors error_count = 0 with open("app.log") as f: for line in f: if "ERROR" in line: error_count += 1 print("Total Errors:", error_count) 🧠 Step 4: Handle multiple log levels (Real-world scenario) error = 0 warning = 0 info = 0 with open("app.log") as f: for line in f: if "ERROR" in line: error += 1 elif "WARNING" in line: warning += 1 elif "INFO" in line: info += 1 print("ERROR:", error) print("WARNING:", warning) print("INFO:", info) 🚨 Step 5: DevOps-style alert output with open("app.log") as f: for line in f: if "ERROR" in line: print("ALERT:", line.strip()) 💡 Key Learning: Python is a powerful tool in DevOps for log monitoring, automation, and faster troubleshooting. 🔥 Even simple scripts like this can help in: Production monitoring CI/CD pipelines Incident detection 📈 Next Goal: Build a real-time log monitoring script (like tail -f) using Python #Python #DevOps #Automation #Scripting #Learning #Cloud #100DaysOfCode

To view or add a comment, sign in

Explore content categories