From a9b70be57c4ec3fe9b2d00191a207ceab3f8d3fe Mon Sep 17 00:00:00 2001 From: "Benjamin Arntzen (aider)" Date: Thu, 27 Jun 2024 07:47:00 +0100 Subject: [PATCH] Improved log parsing by checking for valid JSON and applying transformations to handle different log formats. --- server.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/server.js b/server.js index c090421..3fc11e1 100644 --- a/server.js +++ b/server.js @@ -101,19 +101,25 @@ function parseLogLines(data) { const logLines = data.split('\n'); const validLogs = []; - logLines.forEach((line, index) => { if (line.trim() !== '') { try { - // Transform log line into valid JSON - let jsonLine = line - .replace(/\\\\"/g, '\\\\\\"') // Escape sequences of backslash+quote - .replace(/(\w+)=/g, '"$1":') // Replace '=' with ':' to form valid JSON - .replace(/,(\s*})/g, '$1') // Remove trailing commas before closing braces - .replace(/_stream":"{(.+?)}"/g, (match, p1) => `_stream":{"${p1.replace(/=/g, '":"').replace(/,/g, '","')}"}`) // Transform _stream field into a JSON object - .replace(/_msg":"(.+?)"/g, (match, p1) => `_msg":"${p1.replace(/([a-zA-Z0-9]+)=/g, '$1:').replace(/([a-zA-Z0-9]+): /g, '"$1": ').replace(/, /g, ',').replace(/([a-zA-Z0-9]+):/g, '"$1":')}"`); // Transform _msg field into a JSON object - const parsedLog = JSON.parse(jsonLine); - validLogs.push(parsedLog); + // Check if the line is already valid JSON + try { + const parsedLog = JSON.parse(line); + validLogs.push(parsedLog); + } catch { + // If it's not valid JSON, apply transformations + let jsonLine = line + .replace(/\\"/g, '"') // Replace escaped quotes with regular quotes + .replace(/(\w+)=/g, '"$1":') // Replace '=' with ':' to form valid JSON + .replace(/,(\s*})/g, '$1') // Remove trailing commas before closing braces + .replace(/_stream":"{(.+?)}"/g, (match, p1) => `_stream":{"${p1.replace(/=/g, '":"').replace(/,/g, '","')}"}`) // Transform _stream field into a JSON object + .replace(/_msg":"(.+?)"/g, (match, p1) => `_msg":"${p1.replace(/([a-zA-Z0-9]+)=/g, '$1:').replace(/([a-zA-Z0-9]+): /g, '"$1": ').replace(/, /g, ',').replace(/([a-zA-Z0-9]+):/g, '"$1":')}"`); // Transform _msg field into a JSON object + + const parsedLog = JSON.parse(jsonLine); + validLogs.push(parsedLog); + } } catch (parseError) { console.error(`Error parsing log line at index ${index}: ${parseError.message}`); console.error(`Log line: ${line}`);