Created
November 8, 2011 20:35
-
-
Save mklabs/1349107 to your computer and use it in GitHub Desktop.
minilog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var util = require('util'); | |
var levels = [ | |
'error', | |
'warn', | |
'info', | |
'log', | |
'debug' | |
]; | |
var colors = [ | |
31, | |
33, | |
36, | |
32, | |
90 | |
]; | |
var Logger = function(opts) { | |
opts = opts || {}; | |
this.colors = opts.colors || true; | |
this.level = 4; | |
this.enabled = true; | |
}; | |
Logger.prototype.out = function(type) { | |
var index = levels.indexOf(type), | |
prefix = this.colors ? ' \033[' + colors[index] + 'm' + pad(type) + ' -\033[39m' : type + ':', | |
args = toArray(arguments); | |
if (index > this.level || !this.enabled) | |
return this; | |
args = args.map(function(arg) { | |
return typeof arg === 'object' ? inspect(arg, prefix) : arg; | |
}); | |
console.log.apply(console, [prefix].concat(args.slice(1))); | |
return this; | |
}; | |
levels.forEach(function(name) { | |
Logger.prototype[name] = function () { | |
this.out.apply(this, [name].concat(toArray(arguments))); | |
}; | |
}); | |
module.exports = new Logger; | |
// ## helpers | |
function pad(str) { | |
var max = 0; | |
for (var i = 0, l = levels.length; i < l; i++) | |
max = Math.max(max, levels[i].length); | |
if (str.length < max) | |
return str + new Array(max - str.length + 1).join(' '); | |
return str; | |
}; | |
function toArray(o) { | |
return Array.prototype.slice.call(o); | |
}; | |
function inspect(data, prefix) { | |
return ('\n' + util.inspect(data, false, 4, true) + '\n').split('\n').map(function(line) { | |
return !!line ? prefix + line : line; | |
}).join('\n'); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var console = require('./logger.js'); | |
console.debug('Foo', 'Bar', 'foobar', 'baz.'); | |
console.log('Foo', 'Bar', 'foobar', 'baz.'); | |
console.info('Foo', 'Bar', 'foobar', module, 'baz.'); | |
console.info('Foo', 'Bar', 'foobar', module, module, module, 'baz.'); | |
console.warn('Foo', 'Bar', 'foobar', 'baz.'); | |
console.error('Foo', 'Bar', 'foobar', 'baz.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment