Python Log Monitoring with File Output Automation

🚀 Python for DevOps – Log Monitoring with File Output Today I built a simple automation script to read logs and write alerts to a separate file. 📂 Scenario: Instead of manually checking logs, automate detection of ERROR messages and store them in another file. 💻 Python Code: with open("app.log") as f, open("alerts.log", "w") as out: for line in f: if "ERROR" in line: out.write(line) output: root@satheesha:~# python3 Python 3.12.3 (main, Mar 3 2026, 12:15:18) [GCC 13.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> with open("app.log") as f, open("Alert.log", "w") as out: ...   for line in f: ...     if "ERROR" in line: ...       out.write(line) ...  17 >>> exit() root@satheesha:~# cat Alert.log ERROR: Disk full 🔍 What this does: Reads app.log line by line Filters only ERROR logs Writes them into alerts.log 📌 Why this is useful: Helps in faster troubleshooting Reduces manual log scanning Can be integrated with monitoring systems 🔥 Real DevOps Use Cases: Production log monitoring CI/CD pipeline validation Incident detection and alerting 📈 Next Step: Enhance this script to: Handle multiple log levels (ERROR / WARNING / INFO) Send alerts to email or Slack Monitor logs in real-time (like tail -f) #Python #DevOps #Automation #Scripting #Cloud #Learning #100DaysOfCode

To view or add a comment, sign in

Explore content categories