Awesome Open Source
Awesome Open Source

filter-console

Filter out unwanted console.log() output

Can be useful when you don't control the output, for example, filtering out PropType warnings from a third-party React component.

Install

$ npm install filter-console

Usage

const filterConsole = require('filter-console');

const disableFilter = filterConsole(['šŸ¼']);

const log = () => {
	console.log('');
	console.log('šŸ¦„');
	console.log('šŸ¼');
	console.log('šŸ¶');
};

log();

disableFilter();

log();
$ node example.js

šŸ¦„
šŸ¶

šŸ¦„
šŸ¼
šŸ¶

API

filterConsole(excludePatterns, [options])

Returns a function, which when called, disables the filter.

excludePatterns

Type: Array<string|RegExp|Function>

Console output that matches any of the given patterns are filtered from being logged. The patterns are matched against what would be logged and not the console method input arguments directly. Meaning an exclude pattern of 'foo bar' will match console.log('foo %s', 'bar').

Filter types:

  • string: Checks if the string pattern is included in the console output.
  • RegExp: Checks if the RegExp pattern matches the console output.
  • Function: Receives the console output as a string and is expected to return a truthy/falsy value of whether to exclude it.

options

Type: Object

methods

Type: Array
Default: ['log', 'debug', 'info', 'warn', 'error']

Console methods to filter.

console

Type: Object
Default: console

Use a custom console object. Can be useful for testing or mocking.

License

MIT Ā© Sindre Sorhus


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
javascript (70,970)Ā 
nodejs (3,873)Ā 
browser (471)Ā 
npm-package (264)Ā 
console (261)Ā 
filter (210)Ā 
devtools (136)Ā