-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
312 lines (261 loc) · 10.5 KB
/
Copy pathparser.py
File metadata and controls
312 lines (261 loc) · 10.5 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""
parser.py — Format-adaptive log structure detector and deterministic parser.
Detects log patterns (JSON-lines, CSV, Key-Value, Space-Delimited, Bracketed)
and extracts structured events (timestamp, level, message) without hardcoded
schema assumptions.
"""
import csv
import json
import re
from datetime import datetime, timezone
# ---------------------------------------------------------------------------
# Regex Constants for Detection
# ---------------------------------------------------------------------------
# Timestamp Patterns
_TS_PATTERNS = [
# Bracketed ISO 8601: [2026-08-22T14:22:45Z] or [2026-08-22 14:22:45]
(re.compile(r"\[(?P<ts>\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?)\]"), "bracketed_iso"),
# Standard ISO 8601 / Date-Time: 2026-08-22T14:22:45Z or 2026-08-22 14:22:45
(re.compile(r"\b(?P<ts>\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?)\b"), "iso_dateTime"),
# Common Slash Date-Time: 22/Aug/2026:14:22:45 or 2026/08/22 14:22:45
(re.compile(r"\b(?P<ts>\d{4}/\d{2}/\d{2}[ T]\d{2}:\d{2}:\d{2})\b"), "slash_dateTime"),
# Unix Epoch Timestamp (seconds/millis): e.g. 1787361765 or 1787361765.123
(re.compile(r"\b(?P<ts>1\d{9}(?:\.\d+)?)\b"), "epoch"),
]
# Log Level Patterns
_LEVEL_RE = re.compile(
r"\b(?:\[)?(?P<level>INFO|WARN|WARNING|ERROR|ERR|DEBUG|CRITICAL|FATAL|SEVERE)(?:\])?\b",
re.IGNORECASE,
)
def _parse_ts_str(ts_str: str) -> datetime | None:
"""Best-effort parser for common timestamp string formats."""
clean = ts_str.strip("[]\"'").replace("T", " ").rstrip("Z")
# Safely strip ISO timezone offset (+05:00, -05:00, +0500) if present at end of string
clean = re.sub(r"(?:[+-]\d{2}:?\d{2})$", "", clean).strip()
# Truncate fractional seconds if present
if "." in clean:
parts = clean.split(".")
clean = parts[0]
clean = clean.strip()
formats = [
"%Y-%m-%d %H:%M:%S",
"%Y/%m/%d %H:%M:%S",
"%Y-%m-%d",
]
for fmt in formats:
try:
return datetime.strptime(clean, fmt).replace(tzinfo=timezone.utc)
except ValueError:
pass
# Try epoch float
try:
val = float(clean)
if val > 1e11: # milliseconds
val /= 1000.0
return datetime.fromtimestamp(val, tz=timezone.utc)
except ValueError:
pass
return None
def detect_log_structure(sample_lines: list[str]) -> dict:
"""
Examine a sample of log lines to detect format structure.
Returns dict:
{
"format": "json_lines" | "csv" | "key_value" | "space_delimited" | "unknown",
"timestamp_pattern": str or None,
"level_pattern": str or None,
"delimiter": str or None,
"json_keys": {"ts": ..., "level": ..., "msg": ...} (for json_lines),
"csv_indices": {"ts": ..., "level": ..., "msg": ...} (for csv)
}
"""
lines = [l.strip() for l in sample_lines if l.strip()]
if not lines:
return {"format": "unknown"}
# 1. Check if JSON-lines
json_valid_count = 0
detected_json_keys = {}
for l in lines[:10]:
if l.startswith("{") and l.endswith("}"):
try:
data = json.loads(l)
if isinstance(data, dict):
json_valid_count += 1
for k in data.keys():
kl = k.lower()
if kl in ("timestamp", "time", "ts", "@timestamp", "date"):
detected_json_keys["ts"] = k
elif kl in ("level", "severity", "log_level", "lvl", "status"):
detected_json_keys["level"] = k
elif kl in ("message", "msg", "log", "text", "event", "detail"):
detected_json_keys["msg"] = k
except Exception:
pass
if json_valid_count >= len(lines[:10]) * 0.7 and json_valid_count > 0:
return {
"format": "json_lines",
"timestamp_pattern": "json",
"level_pattern": "json",
"delimiter": None,
"json_keys": detected_json_keys,
}
# 2. Check if CSV format
csv_count = 0
csv_indices = {}
for l in lines[:10]:
if "," in l:
parts = list(csv.reader([l]))[0]
if len(parts) >= 3:
csv_count += 1
for idx, p in enumerate(parts):
p_str = p.strip()
if _parse_ts_str(p_str) is not None and "ts" not in csv_indices:
csv_indices["ts"] = idx
elif _LEVEL_RE.search(p_str) and "level" not in csv_indices:
csv_indices["level"] = idx
if "msg" not in csv_indices and len(parts) > 2:
used = {csv_indices.get("ts"), csv_indices.get("level")}
for idx in range(len(parts)):
if idx not in used:
csv_indices["msg"] = idx
break
if csv_count >= len(lines[:10]) * 0.7 and "ts" in csv_indices:
return {
"format": "csv",
"timestamp_pattern": "csv",
"level_pattern": "csv",
"delimiter": ",",
"csv_indices": csv_indices,
}
# 3. Check if Key-Value pairs (e.g. time=... level=... msg=...)
kv_count = 0
for l in lines[:10]:
if len(re.findall(r"\b\w+=[^\s]+", l)) >= 2:
kv_count += 1
if kv_count >= len(lines[:10]) * 0.7:
return {
"format": "key_value",
"timestamp_pattern": "kv",
"level_pattern": "kv",
"delimiter": " ",
}
# 4. Standard Delimited / Text Format Detection
ts_found = False
ts_pat_name = None
level_found = False
for l in lines[:10]:
for pat, pat_name in _TS_PATTERNS:
if pat.search(l):
ts_found = True
ts_pat_name = pat_name
break
if _LEVEL_RE.search(l):
level_found = True
if ts_found or level_found:
return {
"format": "space_delimited",
"timestamp_pattern": ts_pat_name,
"level_pattern": "level_regex",
"delimiter": " ",
}
return {"format": "unknown"}
def parse_with_detected_structure(raw_log_text: str, structure: dict) -> list:
"""
Parse raw log text using detected structure format.
Returns list of dicts:
[{"ts": datetime, "level": str, "message": str, "raw": str}, ...]
"""
lines = [l.rstrip() for l in raw_log_text.splitlines() if l.strip()]
results = []
fmt = structure.get("format", "unknown")
# --- Mode 1: JSON-lines ---
if fmt == "json_lines":
keys = structure.get("json_keys", {})
ts_key = keys.get("ts", "ts")
level_key = keys.get("level", "level")
msg_key = keys.get("msg", "msg")
for l in lines:
try:
data = json.loads(l)
if not isinstance(data, dict):
continue
ts_raw = str(data.get(ts_key) or data.get("timestamp") or data.get("time") or "")
ts = _parse_ts_str(ts_raw) or datetime.now(tz=timezone.utc)
lvl = str(data.get(level_key) or data.get("severity") or "INFO").upper()
if lvl == "WARNING":
lvl = "WARN"
elif lvl in ("CRITICAL", "FATAL", "SEVERE"):
lvl = "ERROR"
msg = str(data.get(msg_key) or data.get("message") or data.get("log") or str(data))
results.append({"ts": ts, "level": lvl, "message": msg, "raw": l})
except Exception:
pass
return results
# --- Mode 2: CSV ---
if fmt == "csv":
indices = structure.get("csv_indices", {})
ts_idx = indices.get("ts", 0)
lvl_idx = indices.get("level", 1)
msg_idx = indices.get("msg", 2)
for l in lines:
try:
parts = list(csv.reader([l]))[0]
if len(parts) <= max(ts_idx, lvl_idx):
continue
ts_str = parts[ts_idx].strip()
ts = _parse_ts_str(ts_str) or datetime.now(tz=timezone.utc)
lvl = parts[lvl_idx].strip().upper() if len(parts) > lvl_idx else "INFO"
if lvl == "WARNING":
lvl = "WARN"
elif lvl in ("CRITICAL", "FATAL", "SEVERE"):
lvl = "ERROR"
elif not _LEVEL_RE.match(lvl):
lvl = "INFO"
msg = parts[msg_idx].strip() if len(parts) > msg_idx else l
results.append({"ts": ts, "level": lvl, "message": msg, "raw": l})
except Exception:
pass
return results
# --- Mode 3: Key-Value ---
if fmt == "key_value":
for l in lines:
kv_pairs = dict(re.findall(r'(\w+)=(?:"([^"]*)"|(\S+))', l))
kv_flat = {k: v1 or v2 for k, (v1, v2) in kv_pairs.items()}
ts_raw = kv_flat.get("time") or kv_flat.get("ts") or kv_flat.get("timestamp") or ""
ts = _parse_ts_str(ts_raw) or datetime.now(tz=timezone.utc)
lvl = (kv_flat.get("level") or kv_flat.get("lvl") or "INFO").upper()
if lvl == "WARNING":
lvl = "WARN"
elif lvl in ("CRITICAL", "FATAL", "SEVERE"):
lvl = "ERROR"
msg = kv_flat.get("msg") or kv_flat.get("message") or l
results.append({"ts": ts, "level": lvl, "message": msg, "raw": l})
return results
# --- Mode 4: Delimited Text / Regex Slicing ---
for l in lines:
ts = None
ts_match_end = 0
for pat, _ in _TS_PATTERNS:
m = pat.search(l)
if m:
ts = _parse_ts_str(m.group("ts"))
ts_match_end = m.end()
break
if not ts:
ts = datetime.now(tz=timezone.utc)
level_m = _LEVEL_RE.search(l)
if level_m:
lvl = level_m.group("level").upper()
if lvl == "WARNING":
lvl = "WARN"
elif lvl in ("CRITICAL", "FATAL", "SEVERE"):
lvl = "ERROR"
msg_start = max(ts_match_end, level_m.end())
msg = l[msg_start:].strip(" :]-")
else:
lvl = "INFO"
msg = l[ts_match_end:].strip(" :]-")
if not msg:
msg = l
results.append({"ts": ts, "level": lvl, "message": msg, "raw": l})
return results