minilog

Filters

Filtering has been significantly improved in Minilog v2 to accomodate larger apps. This is done via Minilog.suggest.

In Minilog, filters are duplex streams (readable and writable) which selectively discard messages.

Filters can be applied to each pipe individually:

Minilog
  .pipe(filter)
  .pipe(process.stdout)

Minilog.suggest is an instance of Minilog.Filter that's enabled by default.

Specifically, calling Minilog.enable() does this:

Minilog.pipe(Minilog.suggest) // filter
       .pipe(Minilog.defaultFormatter) // formatter
       .pipe(Minilog.defaultBackend); // backend - e.g. the console

The benefit to this is that it allows modules to set a suggested log level.

Suggesting a log level in a library. This is useful if you want to use Minilog for logging in some low-level library which you normally do not care but still want to have the ability to turn on logging for explicitly if it is needed. For example, you might have a module that has a lot of internal logging that's not relevant to it's users. Inside a module, you could do:

Minilog.suggest.deny(/mymodule\/.*/, 'warn');

This configures the default filter to hide logs where the level is < 'warn'. This assumes you are namespacing your logs with the prefix "mymodule/" in your module.

Configuring a global logging level. You can use the same Minilog.suggest mechanism to filter out (or include) logs at the app level - nothing forces you to use the suggested settings. For example:

Minilog
  .suggest
    .clear()
    .deny('foo', 'warn');
Minilog.enable();

Using .clear() discards any default settings. If you want a whitelist-based approach rather than a blacklist-based one, try setting .suggest.defaultResult:

Minilog.suggest.defaultResult = false;
Minilog
  .suggest
  .clear()
  .allow('bar', 'info');
Minilog.enable();

Minilog.Filter

Minilog.Filter is a simple whitelist & blacklist -based filter with the following API:

  • .clear(): empties the whitelist and blacklist
  • .allow(name, level): adds an entry to the whitelist
  • .deny(name, level): adds an entry to the blacklist
  • .defaultResult: specifies the behavior when a log line doesn't match either the whitelist or the blacklist. The default is true (= "allow by default") - lines that do not match the whitelist or the blacklist are not filtered (e.g. ). If you want to flip the default so that lines are filtered unless they are on the whitelist, set this to false (= "deny by default").
  • .enabled: boolean, controls whether the filter is enabled. Default: true

For even greater control over the filter, you can define your own filter. For example:

var myFilter = new Minilog.Filter();
// allow any logs from the namespace/module "foo", level >= 'info
myFilter.allow('foo', 'debug');
// deny any logs where the module name matches "bar.*", level < 'warn'
// e.g. only let through "warn" and "error"
myFilter.deny(new RegExp('bar.*', 'warn');

// now, create a custom pipe
Minilog.pipe(myFilter)
       .pipe(Minilog.defaultFormatter)
       .pipe(Minilog.defaultBackend);

The whitelist is always applied first, then the blacklist.