Refactored log line parsing to improve JSON transformation and parsing.

This commit is contained in:
Benjamin Arntzen
2024-06-27 07:23:51 +01:00
committed by Benjamin Arntzen (aider)
parent 7f8489ba1d
commit ecd29eaf77
+8 -18
View File
@@ -105,24 +105,14 @@ function parseLogLines(data) {
logLines.forEach((line, index) => {
if (line.trim() !== '') {
try {
let jsonLine = line.trim();
// Replace the custom log format with valid JSON format
jsonLine = jsonLine.replace(/(\w+)="(.*?)"/g, '"$1":"$2"');
jsonLine = `{${jsonLine}}`;
jsonLine = jsonLine.replace(/"(\w+)":{/g, '"$1":{');
jsonLine = jsonLine.replace(/,(\w+):/g, ',"$1":');
jsonLine = jsonLine.replace(/(\w+):/g, '"$1":');
.replace(/=\{/g, ':{') // Replace ={ with :{
.replace(/\}([,}])/g, '}$1') // Replace } with } at the end of objects
.replace(/=/g, '":') // Replace = with ":
.replace(/,(\S)/g, ',"$1') // Replace , with ," to separate fields
.replace(/(\w+):/g, '"$1":') // Add quotes around field names
.replace(/"(\w+)":{/g, '"$1":\{') // Ensure that nested objects have correct format
.replace(/(\w+)"(\w+)":/g, '$1"$2":') // Fix fields that may have been incorrectly split
.replace(/""/g, '\\"') // Escape double quotes inside strings
.replace(/(\w+):"/g, '"$1":"') // Ensure that all values are enclosed in double quotes
.replace(/(\w+)"(\w+)"/g, '$1"$2"') // Fix values that may have been incorrectly split
.replace(/""/g, '"'); // Remove any double double-quotes introduced by previous replacements
let jsonLine = line.trim()
.replace(/(\w+)="(.*?)"/g, '"$1":"$2"') // Replace key="value" with "key":"value"
.replace(/([,{])(\w+)=/g, '$1"$2":') // Add quotes around unquoted keys
.replace(/,(\w+):/g, ',"$1":') // Ensure commas are present between fields
.replace(/}(\w+):/g, '},"$1":'); // Ensure commas are present between nested objects
// Parse the transformed JSON string
const parsedLog = JSON.parse(jsonLine);
// Parse the transformed JSON string
const parsedLog = JSON.parse('{' + jsonLine + '}');