Q4
Scenario 4: Write a script that parses an application log file, extracts all "ERROR" entries, and generates a formatted markdown report.
💬Answer
log_file = "app.log"
report_file = "error_report.md"
errors = []
# 1. Read and find errors
with open(log_file, "r") as f:
for line in f:
if "ERROR" in line:
errors.append(line.strip())
# 2. Write Markdown report
with open(report_file, "w") as f:
f.write("# 📋 DevOps Log Incident Report\n\n")
f.write(f"**Source Log File:** `{log_file}` \n")
f.write(f"**Total Incidents Found:** {len(errors)} \n\n")
f.write("## ⚠️ Identified Exceptions:\n")
if errors:
for idx, error in enumerate(errors, 1):
f.write(f"{idx}. `{error}`\n")
else:
f.write("*No errors detected in the current log range.*\n")
print(f"Report compiled successfully at {report_file}")
Related Python Questions
View All PythonQuestions →
Created by
Apurv Gujjar
DevOps & Cloud Engineer
Specialized in:DevOpsAWSGCPKubernetesTerraformDocker
View Portfolio