const fs = require('fs');
/**
* Configures log file error suppression with manual overrides.
*
* @param {string} logFilePath - The path to the log file.
* @param {Array<string>} ignorePatterns - An array of glob patterns to ignore (e.g., 'error', 'warn').
* @param {Function} overrideFunction - A function that takes the log message and returns whether to log it or not.
*/
function configureLogSuppression(logFilePath, ignorePatterns, overrideFunction) {
// Read the existing log file content
fs.readFile(logFilePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading log file: ${err}`);
return;
}
const lines = data.split('\n');
const filteredLines = lines.filter(line => {
// Check if the line should be ignored based on the patterns
const shouldIgnore = ignorePatterns.some(pattern => line.includes(pattern));
// Apply manual override
if (overrideFunction && overrideFunction(line)) {
return true; // Log the line if override function returns true
}
return !shouldIgnore; // Log the line if not ignored
});
// Write the filtered lines back to the log file
fs.writeFile(logFilePath, filteredLines.join('\n'), 'utf8', (err) => {
if (err) {
console.error(`Error writing to log file: ${err}`);
} else {
console.log(`Log file ${logFilePath} processed successfully.`);
}
});
});
}
module.exports = configureLogSuppression;
Add your comment