Globally overwrite console methods - silence all, filter individually.
Just debugging a legacy project and I had too many logs / warnings printing to the console. I started to remove / comment them, but realized they could be useful later on. This module is just an extension of the code I wrote for that use.
npm install --save-dev silentconsole
You can use it at the file level, but it is best for importing at top level (usually your main entry ie index.js) and using for global silencing / filtering of console output.
import silentConsole from 'silentconsole';
silentConsole('log');
silentConsole('warn');
console.log('foo'); // will not print to console
console.warn('foo'); // will not print to console
console.info('foo'); // will print 'foo' to consoleimport silentConsole from 'silentconsole';
silentConsole('log', (text) => !text.includes('bar'));
silentConsole('warn', (text) => !text.includes('baz'));
console.log('foo'); // will print 'foo' to console
console.log('bar'); // will not print to console
console.warn('foo bar'); // will print 'foo bar' to console
console.warn('foo bar baz'); // will not print to consoleoriginal will be added to the console object storing all of the original methods of console.
import silentConsole from 'silentconsole';
silentConsole('log');
console.log('foo'); // will not print to console
console.log('bar'); // will not print to console
console.original.log('foo'); // will print 'foo' to consoleThis is not a great good use.
import silentConsole from 'silentconsole';
silentConsole('log');
console.log('foo'); // will not print to console
console.log('bar'); // will not print to console
silentConsole('log');
console.log('foo'); // will print 'foo' to console
console.log('bar'); // will print 'bar' to console
import silentConsole from 'silentconsole';
silentConsole('log');
console.log('foo'); // will not print to console
console.log('bar'); // will not print to console
silentConsole('log', false);
console.log('foo'); // will print 'foo' to console
console.log('bar'); // will print 'bar' to console
Even if you setup the silentConsole, you can still pass in multiple arguments. The printed results will be a bit different depending on what you pass as last argument.
import silentConsole from 'silentconsole';
silentConsole('log', (text) => !text.includes('test'));
console.log('foo', 'bar', 'baz'); // will print 'foo bar baz' to console
console.log('foo', 'bar'); // will print 'foo bar' to console![]() |
|---|
| Steve Reed |
