/**
* Matches log streams against predefined patterns.
*
* @param {string} logStream The log stream to analyze.
* @param {Array<Object>} patterns An array of pattern objects. Each object has:
* - name: A unique name for the pattern.
* - regex: A regular expression to match against the log stream.
* - data: An object containing data to extract from the matched log stream.
* @param {boolean} dryRun If true, only report matches without outputting extracted data.
* @returns {Array<Object>} An array of matched pattern objects, including the original log stream
*/
function matchLogStreams(logStream, patterns, dryRun = false) {
const matches = [];
for (const pattern of patterns) {
try {
const regex = new RegExp(pattern.regex);
const match = logStream.match(regex);
if (match) {
const matchedData = { ...pattern.data, logStream }; // Combine data with log stream
if (dryRun) {
matches.push({ ...matchedData, matched: true }); // Indicate match without extracting data
console.log(`Dry Run: Pattern '${pattern.name}' matched.`);
} else {
matches.push({ ...matchedData, matched: true });
console.log(`Pattern '${pattern.name}' matched:`, matchedData);
}
}
} catch (error) {
console.error(`Error processing pattern '${pattern.name}':`, error);
}
}
return matches;
}
Add your comment