Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
64 lines (51 loc) · 2.21 KB
/
Copy pathapp.py
File metadata and controls
64 lines (51 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# app.py
# Connect the pipes
from utils.iterators import log_file_reader
from processor.core import pares_parts
from processor.exceptions import CorruptLineError, SystemHealthError
from config import LOG_FILE_PATH
def main():
print("--- Starting Resilience Log Processor ---")
# We'll use these for Phase 4 (Aggregation)
parsed_records = []
# State Management (Dicts & Sets)
stats = {"INFO": 0, "ERROR": 0, "WARNING": 0}
unique_errors = set()
print(f"--- Processing: {LOG_FILE_PATH} ---")
# PHASE 2: Using our 'Lazy' Iterator
for raw_line in log_file_reader(LOG_FILE_PATH):
# Record structure: (Timestamp, Level, ErrorCode, Message)
# OR (Timestamp, Level, Message
# PHASE 3: Exception Handling
try:
# The Brain turns String -> Tuple
record = pares_parts(raw_line)
parsed_records.append(record) # Put the tuple in a list -> or the custom exception
level = record[1] # Access the ERROR faild
# level could be -> INFO | ERROR | WARNING, and this should be knowing basid on the record format.
# Update dictionary using .get() or direct access
stats[level] = stats.get(level, 0) + 1
if level == "ERROR":
unique_errors.add(record[2]) # If it's an error -> We need the error message.
except CorruptLineError as e:
# We catch the specific error, log it, and CONTINUE the loop
print(f"Skipping bad data: {e}")
continue
except Exception as e:
# A safety net for unexpected disasters
print(f"Unexpected system error: {e}")
break
if stats["ERROR"] > 3:
raise SystemHealthError(f"CRITICAL: {stats['ERROR']} errors detected! Shutting donw.")
print(f"--- SUMMARY REPORT ---")
print(f"Counts: {stats}")
print(f"UNIQUE ERROR TYPES: {unique_errors}")
print(f"--- Successfully parsed {len(parsed_records)} records ---")
return parsed_records
if __name__ == "__main__":
try:
main()
except SystemHealthError as e:
print(f"\n🛑 SHUTDOWN: {e}")
except Exception as e:
print(f"\n⚠️ UNEXPECTED CRASH: {e}")