fix regex matching

This commit is contained in:
2026-04-29 11:46:23 +07:00
parent 3141a14036
commit deaff4531c

View File

@@ -908,34 +908,30 @@
// Try common log formats // Try common log formats
const patterns = [ const patterns = [
// 2024-01-15T10:23:45.123Z INFO message // 2024-01-15T10:23:45.123Z INFO message
/^(\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?)\s+(DEBUG|INFO|WARN(?:ING)?|ERROR|FATAL|TRACE)\s+(.+)$/i, /^(?<time>\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?)\s+(?<lvl>DEBUG|INFO|WARN(?:ING)?|ERROR|FATAL|TRACE)\s+(?<msg>.+)$/i,
// [2024-01-15 10:23:45] [INFO] message // [2024-01-15 10:23:45] [INFO] message
/^\[(\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:\.\d+)?)\]\s*\[(DEBUG|INFO|WARN(?:ING)?|ERROR|FATAL|TRACE)\]\s*(.+)$/i, /^\[(?<time>\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:\.\d+)?)\]\s*\[(?<lvl>DEBUG|INFO|WARN(?:ING)?|ERROR|FATAL|TRACE)\]\s*(?<msg>.+)$/i,
// Jan 15 10:23:45 INFO message // Jan 15 10:23:45 INFO message
/^(\w{3}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2})\s+(DEBUG|INFO|WARN(?:ING)?|ERROR|FATAL)\s+(.+)$/i, /^(?<time>\w{3}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2})\s+(?<lvl>DEBUG|INFO|WARN(?:ING)?|ERROR|FATAL)\s+(?<msg>.+)$/i,
// INFO 2024-01-15 10:23:45 message // INFO 2024-01-15 10:23:45 message
/^(DEBUG|INFO|WARN(?:ING)?|ERROR|FATAL)\s+(\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:\.\d+)?)\s+(.+)$/i, /^(?<lvl>DEBUG|INFO|WARN(?:ING)?|ERROR|FATAL)\s+(?<time>\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:\.\d+)?)\s+(?<msg>.+)$/i,
// 10:23:43.333 [INF] message // 10:23:43.333 [INF] message (props)
/^(\d{2}:\d{2}:\d{2}\.\d{3})\s\[(INF|WRN|ERR|CRT|DBG|TRC)\]\s(.*)$/ /^(?<time>\d{2}:\d{2}:\d{2}\.\d{3})\s\[(?<lvl>INF|WRN|ERR|CRT|DBG|TRC)\]\s(?<msg>.*?)(?:\s\((?<props>".*?")\))?$/i
]; ];
let i = 0; let i = 0;
//debugger debugger
while (i < lines.length) { while (i < lines.length) {
const line = lines[i].trim(); const line = lines[i].trim();
let matched = false; let matched = false;
for (const pat of patterns) { for (const pat of patterns) {
const m = line.match(pat); const m = line.match(pat);
if (m) { if (m) {
const [, g1, g2, g3] = m; let timeStr = m.groups.time || null
let timeStr, levelStr, msg; let levelStr = translateLogLevel(m.groups.lvl || '')
if (/^(DEBUG|INFO|WARN|ERROR|FATAL)/i.test(g1)) { let msg = m.groups.msg || null
timeStr = g2; levelStr = g1.toUpperCase().replace('WARNING', 'WARN'); msg = g3; let props = m.groups.props || null
} else {
timeStr = g1; levelStr = g2.toUpperCase().replace('WARNING', 'WARN'); msg = g3;
}
levelStr = translateLogLevel(levelStr)
let time
if (filedate === null) { if (filedate === null) {
time = new Date(timeStr) time = new Date(timeStr)
} }
@@ -946,6 +942,7 @@
} }
if (!isNaN(time)) { if (!isNaN(time)) {
// Collect continuation lines (stack traces etc.) // Collect continuation lines (stack traces etc.)
let exception = ''; let exception = '';
let j = i + 1; let j = i + 1;
@@ -954,13 +951,14 @@
if (cl) exception += (exception ? '\n' : '') + cl; if (cl) exception += (exception ? '\n' : '') + cl;
j++; j++;
} }
i = j - 1; i = j - 1;
logs.push({ logs.push({
time, time,
level: levelStr, level: levelStr,
message: msg.length > msg_len_limit ? msg.slice(0, msg_len_limit) + "..." : msg.trim(), message: msg.length > msg_len_limit ? msg.slice(0, msg_len_limit) + "..." : msg.trim(),
fullmessage: msg.length > msg_len_limit ? msg : null, fullmessage: msg.length > msg_len_limit ? msg : null,
props: null, props: props,
exception: exception || null exception: exception || null
}); });
matched = true; matched = true;