What is filter-console?
The filter-console npm package allows you to filter out unwanted console output in Node.js applications. This can be particularly useful for suppressing noisy logs or warnings that are not relevant to your current debugging or logging needs.
What are filter-console's main functionalities?
Filter specific messages
This feature allows you to filter out specific messages from the console output. In the example, any console warnings or errors that match the specified patterns will be suppressed.
const filterConsole = require('filter-console');
filterConsole(['Warning: ...', 'Deprecation notice: ...']);
console.log('This will be shown');
console.warn('Warning: This will be filtered');
console.error('Deprecation notice: This will also be filtered');
Filter using regular expressions
This feature allows you to use regular expressions to filter out console messages. This is useful for more complex filtering needs where the exact message may vary.
const filterConsole = require('filter-console');
filterConsole([/Warning: .*/, /Deprecation notice: .*/]);
console.log('This will be shown');
console.warn('Warning: This will be filtered');
console.error('Deprecation notice: This will also be filtered');
Unfilter messages
This feature allows you to temporarily filter messages and then unfilter them when needed. The `unfilter` function restores the original console behavior.
const filterConsole = require('filter-console');
const unfilter = filterConsole(['Warning: ...']);
console.log('This will be shown');
console.warn('Warning: This will be filtered');
unfilter();
console.warn('Warning: This will be shown again');
Other packages similar to filter-console
console-filter
The console-filter package provides similar functionality by allowing you to filter out unwanted console messages. It offers more customization options for filtering and formatting the console output compared to filter-console.
console-silencer
The console-silencer package allows you to silence specific console methods like log, warn, and error. It is more focused on silencing entire methods rather than filtering specific messages.
loglevel
The loglevel package provides a simple logging library with support for different log levels (trace, debug, info, warn, error). While it doesn't filter console messages, it allows you to control the verbosity of your logs.
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
import filterConsole from '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: string[]
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.