1. /**
  2. * Matches log streams against predefined patterns.
  3. *
  4. * @param {string} logStream The log stream to analyze.
  5. * @param {Array<Object>} patterns An array of pattern objects. Each object has:
  6. * - name: A unique name for the pattern.
  7. * - regex: A regular expression to match against the log stream.
  8. * - data: An object containing data to extract from the matched log stream.
  9. * @param {boolean} dryRun If true, only report matches without outputting extracted data.
  10. * @returns {Array<Object>} An array of matched pattern objects, including the original log stream
  11. */
  12. function matchLogStreams(logStream, patterns, dryRun = false) {
  13. const matches = [];
  14. for (const pattern of patterns) {
  15. try {
  16. const regex = new RegExp(pattern.regex);
  17. const match = logStream.match(regex);
  18. if (match) {
  19. const matchedData = { ...pattern.data, logStream }; // Combine data with log stream
  20. if (dryRun) {
  21. matches.push({ ...matchedData, matched: true }); // Indicate match without extracting data
  22. console.log(`Dry Run: Pattern '${pattern.name}' matched.`);
  23. } else {
  24. matches.push({ ...matchedData, matched: true });
  25. console.log(`Pattern '${pattern.name}' matched:`, matchedData);
  26. }
  27. }
  28. } catch (error) {
  29. console.error(`Error processing pattern '${pattern.name}':`, error);
  30. }
  31. }
  32. return matches;
  33. }

Add your comment