-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy patherror_logger.py
More file actions
84 lines (67 loc) · 2.64 KB
/
Copy patherror_logger.py
File metadata and controls
84 lines (67 loc) · 2.64 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from __future__ import annotations
import json
import traceback
from collections import Counter
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
DEFAULT_LOG_PATH = Path(__file__).resolve().parent / "logs" / "error-logs.jsonl"
def _ensure_log_path(log_path: str | Path | None) -> Path:
path = Path(log_path) if log_path is not None else DEFAULT_LOG_PATH
path.parent.mkdir(parents=True, exist_ok=True)
return path
def log_exception(
project_name: str,
exception: Exception,
log_path: str | Path | None = None,
additional_data: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""Write a structured exception entry to a JSON Lines log file."""
path = _ensure_log_path(log_path)
payload = {
"project_name": project_name,
"exception_type": type(exception).__name__,
"error_message": str(exception),
"timestamp": datetime.now(timezone.utc).isoformat(),
"traceback": "".join(traceback.format_exception(type(exception), exception, exception.__traceback__)),
}
if additional_data:
payload["additional_data"] = additional_data
with path.open("a", encoding="utf-8") as handle:
handle.write(json.dumps(payload, ensure_ascii=False) + "\n")
return payload
def summarize_logs(log_path: str | Path | None = None) -> dict[str, Any]:
"""Return a compact summary of the stored error logs."""
path = _ensure_log_path(log_path)
if not path.exists():
return {
"total_errors": 0,
"exception_counts": {},
"project_counts": {},
"latest_errors": [],
}
exception_counts: Counter[str] = Counter()
project_counts: Counter[str] = Counter()
latest_errors: list[dict[str, Any]] = []
with path.open("r", encoding="utf-8") as handle:
for line in handle:
line = line.strip()
if not line:
continue
payload = json.loads(line)
exception_counts[payload["exception_type"]] += 1
project_counts[payload["project_name"]] += 1
latest_errors.append(payload)
return {
"total_errors": sum(exception_counts.values()),
"exception_counts": dict(exception_counts),
"project_counts": dict(project_counts),
"latest_errors": latest_errors[-5:],
}
def safe_run(project_name: str, action, log_path: str | Path | None = None):
"""Run an action while logging any unexpected exceptions."""
try:
return action()
except Exception as exc: # pylint: disable=broad-except
log_exception(project_name, exc, log_path=log_path)
raise