Skip to content

Commit

Permalink
Colorful stdout, with ability to turn colors off/on
Browse files Browse the repository at this point in the history
  • Loading branch information
Couto committed Feb 12, 2012
1 parent f663cfe commit 406fea4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
10 changes: 8 additions & 2 deletions examples/file.js
Expand Up @@ -6,13 +6,19 @@
var Log = require('../lib/log')
, fs = require('fs')
// , stream = fs.createWriteStream(__dirname + '/file.log', { flags: 'a' })
, log = new Log('debug');
, log = new Log('debug').colorful({
DEBUG : '\033[1;30m',
NOTICE: '\033[0;32m'
});

log.debug('a debug message');
log.info('a info message');
log.notice('a notice message');
log.warning('a warning message');

log.colorful(false);

log.error('a error message');
log.critical('a critical message');
log.alert('a alert message');
log.emergency('a emergency message');
log.emergency('a emergency message');
16 changes: 15 additions & 1 deletion lib/log.js
Expand Up @@ -178,7 +178,7 @@ Log.prototype = {
var msg = args[0].replace(/%s/g, function(){
return args[i++];
});
if (this.stream === process.stdout) {
if (this.stream === process.stdout && this.useColors) {
this.stream.write(
exports.colors[levelStr]
+ '[' + new Date + ']'
Expand All @@ -198,6 +198,20 @@ Log.prototype = {
}
},

colorful : function (colors) {
var k;
if (Object.prototype.toString.call(colors) === '[object Object]') {
for (k in colors) {
if (colors.hasOwnProperty(k) && exports.colors.hasOwnProperty(k)) {
exports.colors[k] = colors[k];
}
}
this.useColors = true;
} else if (colors === false) { this.useColors = false; }
else { this.useColors = true; }
return this;
},

/**
* Log emergency `msg`.
*
Expand Down

0 comments on commit 406fea4

Please sign in to comment.