Improved log parsing by checking for valid JSON and applying transformations to handle different log formats.
This commit is contained in:
@@ -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}`);
|
||||
|
||||
Reference in New Issue
Block a user