Skip to content

Commit

Permalink
Switch from debug-like whitelist/blacklist model to more simple filte…
Browse files Browse the repository at this point in the history
…r for lib/node/console backend
  • Loading branch information
mixu committed Jul 24, 2012
1 parent e61edee commit ea65ce1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 36 deletions.
27 changes: 12 additions & 15 deletions lib/node/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var styles = {
'magenta' : ['\033[35m', '\033[39m'],
'red' : ['\033[31m', '\033[39m'],
'yellow' : ['\033[33m', '\033[39m']
};
},
levelMap = { debug: 1, info: 2, warn: 3, error: 4 };

function style(str, style) {
return styles[style][0] + str + styles[style][1];
Expand All @@ -28,22 +29,18 @@ module.exports = {
// filter which allows you to disable logging selectively via process.ENV
// Note: invoke with the name of your ENV or a string
filterEnv: function(envStr) {
var whitelist = [],
blacklist = [];
// Adapted from visionmedia's debug:
(envStr || '')
var whitelist = [], i, expr;
(envStr || '*.debug')
.split(/[\s,]+/)
.forEach(function(name){
name = name.replace('*', '.*?');
if (name[0] === '-') {
blacklist.push(new RegExp('^' + name.substr(1) + '$'));
} else {
whitelist.push(new RegExp('^' + name + '$'));
}
.forEach(function(part) {
expr = part.split('.');
if(expr.length > 2) { expr = [ expr.slice(0, -1).join('.'), expr.slice(-1).join() ]; }
whitelist.push({ topic: new RegExp('^'+expr[0].replace('*', '.*')), level: levelMap[expr[1]] || 1 });
});
function filter(name, level) {
return whitelist.some(function(expr) {
return expr.topic && expr.topic.test(name) && levelMap[level] >= expr.level;
});
function filter(name) {
function match(re) { return re.test(name); }
return !blacklist.some(match) && whitelist.some(match);
}
return filter;
},
Expand Down
2 changes: 1 addition & 1 deletion lib/node/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Minilog = require('./minilog.js');
var Minilog = require('../../minilog.js');

// default formatter for Node (adds \n)
Minilog.format(function(name, level, args) {
Expand Down
21 changes: 6 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,32 +128,23 @@ Note that this functionality is just for the pipe to the browser console. If you

## Filtering - Node

The node_console backend (./backends/node_console.js) comes with a filter that you can enable to toggle global log levels via the DEBUG environment variable.

Setup:
The node_console backend (./backends/node_console.js) comes with a filter that works like the in-browser filter, except it requires that you pass it a value - usually an environment variable:

var MiniLog = require('minilog'),
ConsoleBackend = MiniLog.backends.nodeConsole;

MiniLog
.pipe(ConsoleBackend)
.format(ConsoleBackend.formatWithStack)
// pass the content of the variable - allows you to a custom variable
.filter(ConsoleBackend.filterEnv(process.env.DEBUG));

MiniLog('app').info('Hello world');
MiniLog('foo').info('Hello world');
MiniLog('bar').info('Hello world');

Note that filters are applied to each pipe individually, so if you have two pipes, you need to set the filter on both (or you can use different filters).
.filter(ConsoleBackend.filterEnv(process.env.MYENV));

Examples of whitelisting and blacklisting:
Examples:

$ export DEBUG=app && node whitelist_example.js
app info whitelist_example.js:13 Hello world
$ export DEBUG="*,-app,-bar" && node whitelist_example.js
$ export MYENV="foo.*" && node whitelist_example.js
foo info whitelist_example.js:14 Hello world

Note that filters are applied to each pipe individually, so if you have two pipes, you need to set the filter on both (or you can use different filters).

## Themes - Node

In Node, you can do fancy formatting. The node_console backend has several built-in inspired by [logme](https://github.com/vesln/logme). To enable, configure the format() function:
Expand Down
8 changes: 4 additions & 4 deletions test/example/whitelist_example.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var MiniLog = require('../../minilog'),
ConsoleBackend = require('../../backends/node_console');
ConsoleBackend = require('../../lib/node/console');

console.log('Try running this with:');
console.log('export DEBUG=app && node whitelist_example.js');
console.log('export DEBUG="*,-app,-bar" && node whitelist_example.js');
console.log('export MYENV=app && node whitelist_example.js');
console.log('export MYENV="foo.*,bar.*" && node whitelist_example.js');

MiniLog
.pipe(ConsoleBackend)
.format(ConsoleBackend.formatWithStack)
.filter(ConsoleBackend.filterEnv);
.filter(ConsoleBackend.filterEnv(process.env.MYENV));

MiniLog('app').info('Hello world');
MiniLog('foo').info('Hello world');
Expand Down
2 changes: 1 addition & 1 deletion test/minilog.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var fs = require('fs'),
assert = require('assert'),
MiniLog = require('../index.js');
MiniLog = require('../lib/node/index.js');

function WriteStream() {
var self = this;
Expand Down

0 comments on commit ea65ce1

Please sign in to comment.