* Parses log content as JSON array or JSONL format * Handles multiple formats: JSON array, JSONL, and mixed format with debug logs * @param {string} logContent - The raw log content as a string * @returns {Array|null} Array of parsed log entries, or null if parsing fails
(logContent)
| 551 | * @returns {Array|null} Array of parsed log entries, or null if parsing fails |
| 552 | */ |
| 553 | function parseLogEntries(logContent) { |
| 554 | let logEntries; |
| 555 | |
| 556 | // First, try to parse as JSON array (old format) |
| 557 | try { |
| 558 | logEntries = JSON.parse(logContent); |
| 559 | if (!Array.isArray(logEntries) || logEntries.length === 0) { |
| 560 | throw new Error(`${ERR_PARSE}: Not a JSON array or empty array`); |
| 561 | } |
| 562 | return logEntries; |
| 563 | } catch (jsonArrayError) { |
| 564 | // If that fails, try to parse as JSONL format (mixed format with debug logs) |
| 565 | logEntries = []; |
| 566 | const lines = logContent.split("\n"); |
| 567 | |
| 568 | for (const line of lines) { |
| 569 | const trimmedLine = line.trim(); |
| 570 | if (trimmedLine === "") { |
| 571 | continue; // Skip empty lines |
| 572 | } |
| 573 | |
| 574 | // Handle lines that start with [ (JSON array format) |
| 575 | if (trimmedLine.startsWith("[{")) { |
| 576 | try { |
| 577 | const arrayEntries = JSON.parse(trimmedLine); |
| 578 | if (Array.isArray(arrayEntries)) { |
| 579 | logEntries.push(...arrayEntries); |
| 580 | continue; |
| 581 | } |
| 582 | } catch (arrayParseError) { |
| 583 | // Skip invalid array lines |
| 584 | continue; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | // Skip debug log lines that don't start with { |
| 589 | // (these are typically timestamped debug messages) |
| 590 | if (!trimmedLine.startsWith("{")) { |
| 591 | continue; |
| 592 | } |
| 593 | |
| 594 | // Try to parse each line as JSON |
| 595 | try { |
| 596 | const jsonEntry = JSON.parse(trimmedLine); |
| 597 | logEntries.push(jsonEntry); |
| 598 | } catch (jsonLineError) { |
| 599 | // Skip invalid JSON lines (could be partial debug output) |
| 600 | continue; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | // Return null if we couldn't parse anything |
| 606 | if (!Array.isArray(logEntries) || logEntries.length === 0) { |
| 607 | return null; |
| 608 | } |
| 609 | |
| 610 | return logEntries; |
no test coverage detected