From 300e704d53a2700a28030772c52eb965a8e85cd7 Mon Sep 17 00:00:00 2001 From: "Benjamin Arntzen (aider)" Date: Thu, 27 Jun 2024 07:29:32 +0100 Subject: [PATCH] Refactored log line parsing to transform into valid JSON format. --- server.js | 49 +++++++++++++++++++------------------------------ 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/server.js b/server.js index 12e2ed9..6b07fa0 100644 --- a/server.js +++ b/server.js @@ -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; }