Skip to content

Commit

Permalink
Remove date formatting from formatClean and change filterEnv to accep…
Browse files Browse the repository at this point in the history
…t the environment variable content as a param so you can use variables other than process.env.DEBUG which is already used e.g. by Engine.io
  • Loading branch information
mixu committed Jun 18, 2012
1 parent b2b0bb6 commit eb8f111
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
47 changes: 20 additions & 27 deletions backends/node_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@ var styles = {
'yellow' : ['\033[33m', '\033[39m']
};

var whitelist = [],
blacklist = [];

// Adapted from visionmedia's debug:
(process.env.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 + '$'));
}
});

function style(str, style) {
return styles[style][0] + str + styles[style][1];
}
Expand All @@ -41,23 +26,31 @@ module.exports = {
write: function(str) { console.log(str); },
end: function() {},
// filter which allows you to disable logging selectively via process.ENV
filterEnv: function(name) {
function match(re) {
return re.test(name);
// Note: invoke with the name of your ENV or a string
filterEnv: function(envStr) {
var whitelist = [],
blacklist = [];
// Adapted from visionmedia's debug:
(envStr || '')
.split(/[\s,]+/)
.forEach(function(name){
name = name.replace('*', '.*?');
if (name[0] === '-') {
blacklist.push(new RegExp('^' + name.substr(1) + '$'));
} else {
whitelist.push(new RegExp('^' + name + '$'));
}
});
function filter(name) {
function match(re) { return re.test(name); }
return !blacklist.some(match) && whitelist.some(match);
}
return !blacklist.some(match) && whitelist.some(match);
return filter;
},
// formatting
formatClean: function(name, level, args) {
var d = new Date();
function pad(s) { return (s.toString().length == 1? '0'+s : s); }
return '['
+d.getFullYear()+'-'
+pad(d.getMonth()+1) +'-'
+pad(d.getDay())+' '
+d.toLocaleTimeString()
+'] '
+ (name ? name + ' ' : '')
return (name ? name + ' ' : '')
+ (level ? level + ' ' : '')
+ args.join(' ');
},
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ Setup:
MiniLog
.pipe(ConsoleBackend)
.format(ConsoleBackend.formatWithStack)
.filter(ConsoleBackend.filterEnv);
// 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');
Expand Down

0 comments on commit eb8f111

Please sign in to comment.