1. const fs = require('fs');
  2. /**
  3. * Configures log file error suppression with manual overrides.
  4. *
  5. * @param {string} logFilePath - The path to the log file.
  6. * @param {Array<string>} ignorePatterns - An array of glob patterns to ignore (e.g., 'error', 'warn').
  7. * @param {Function} overrideFunction - A function that takes the log message and returns whether to log it or not.
  8. */
  9. function configureLogSuppression(logFilePath, ignorePatterns, overrideFunction) {
  10. // Read the existing log file content
  11. fs.readFile(logFilePath, 'utf8', (err, data) => {
  12. if (err) {
  13. console.error(`Error reading log file: ${err}`);
  14. return;
  15. }
  16. const lines = data.split('\n');
  17. const filteredLines = lines.filter(line => {
  18. // Check if the line should be ignored based on the patterns
  19. const shouldIgnore = ignorePatterns.some(pattern => line.includes(pattern));
  20. // Apply manual override
  21. if (overrideFunction && overrideFunction(line)) {
  22. return true; // Log the line if override function returns true
  23. }
  24. return !shouldIgnore; // Log the line if not ignored
  25. });
  26. // Write the filtered lines back to the log file
  27. fs.writeFile(logFilePath, filteredLines.join('\n'), 'utf8', (err) => {
  28. if (err) {
  29. console.error(`Error writing to log file: ${err}`);
  30. } else {
  31. console.log(`Log file ${logFilePath} processed successfully.`);
  32. }
  33. });
  34. });
  35. }
  36. module.exports = configureLogSuppression;

Add your comment