Refactored log line parsing to transform into valid JSON format.

This commit is contained in:
Benjamin Arntzen (aider)
2024-06-27 07:29:32 +01:00
parent 14a653a377
commit 300e704d53
+19 -30
View File
@@ -101,37 +101,26 @@ function parseLogLines(data) {
const logLines = data.split('\n');
const validLogs = [];
logLines.forEach((line, index) => {
if (line.trim() !== '') {
try {
// Transform log line to valid JSON by replacing '=' with ':' and adding quotes around field names
const jsonLine = line
.replace(/(\w+)=/g, '"$1":')
.replace(/"(\w+)":/g, (_, match) => `"${match}":`)
.replace(/"(\w+)":/g, (_, match) => `"${match}":`)
.replace(/(\w+):/g, '"$1":')
.replace(/,(\s*})/g, '$1');
const parsedLog = JSON.parse(jsonLine);
validLogs.push(parsedLog);
} catch (parseError) {
console.error(`Error parsing log line at index ${index}: ${parseError.message}`);
}
}
});
return validLogs;
if (typeof value === 'string') {
try {
return JSON.parse(value);
} catch (e) {
return value;
}
}
return value;
});
validLogs.push(parsedLog);
} catch (parseError) {
console.error(`Error parsing log line at index ${index}: ${parseError.message}`);
}
}
});
if (typeof value === 'string') {
try {
return JSON.parse(value);
} catch (e) {
return value;
}
}
return value;
});
validLogs.push(parsedLog);
} catch (parseError) {
console.error(`Error parsing log line at index ${index}: ${parseError.message}`);
}
}
});
return validLogs;
}