-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_logs.py
More file actions
65 lines (49 loc) · 1.84 KB
/
analyze_logs.py
File metadata and controls
65 lines (49 loc) · 1.84 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
65
import json
import sys
from collections import defaultdict
def analyze_logs(log_file):
"""
Analyze structured logs and group by correlation ID
Args:
log_file: Path to log file with JSON logs
"""
correlation_groups = defaultdict(list)
with open(log_file, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
entry = json.loads(line)
except json.JSONDecodeError:
continue
correlation_id = entry.get("correlation_id")
if not correlation_id:
continue
correlation_groups[correlation_id].append(entry)
if not correlation_groups:
print("No structured log entries with correlation IDs found.")
return
for correlation_id, entries in correlation_groups.items():
entries.sort(key=lambda item: item.get("timestamp", ""))
services = []
for entry in entries:
service_name = entry.get("service", entry.get("name", "unknown-service"))
if service_name not in services:
services.append(service_name)
print("=" * 80)
print(f"Correlation ID: {correlation_id}")
print(f"Log Entries : {len(entries)}")
print(f"Services : {', '.join(services)}")
print("Request Flow :")
for entry in entries:
timestamp = entry.get("timestamp", "unknown-time")
service = entry.get("service", entry.get("name", "unknown-service"))
message = entry.get("message", "no-message")
print(f" - {timestamp} | {service} | {message}")
print()
if __name__ == '__main__':
if len(sys.argv) > 1:
analyze_logs(sys.argv[1])
else:
print("Usage: python3 analyze_logs.py <log_file>")