Last active
August 29, 2015 14:02
-
-
Save itsoli/08a3bd1118ee08311ba5 to your computer and use it in GitHub Desktop.
This file contains 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 log = (function() { | |
'use strict'; | |
var log = {}; | |
log.Level = function (level, name) { | |
this.level = level; | |
this.name = name; | |
}; | |
log.Level.prototype.toString = function () { | |
return this.name; | |
}; | |
var levels = [ | |
{name: 'error', type: 'error'}, | |
{name: 'warn', type: 'warn'}, | |
{name: 'info', type: 'log'}, | |
{name: 'debug', type: 'log'}, | |
{name: 'trace', type: 'log'} | |
]; | |
for (var i = 0; i < levels.length; i++) { | |
var obj = new log.Level((i + 1) * 10, levels[i].name.toUpperCase()); | |
log.Level[levels[i].name.toUpperCase()] = obj; | |
levels[i].obj = obj; | |
} | |
log.Level.OFF = new log.Level(Number.MIN_VALUE, 'OFF'); | |
log.Level.ALL = new log.Level(Number.MAX_VALUE, 'ALL'); | |
var bind; | |
if (typeof Function.prototype.bind === 'function') { | |
bind = function (type, prefix) { | |
return console[type].bind(console, prefix); | |
}; | |
} else { | |
bind = function (type, prefix) { | |
return function () { | |
[].unshift.call(arguments, prefix); | |
console[type].apply(console, arguments); | |
}; | |
}; | |
} | |
log.Logger = function (name, level) { | |
this.name = name; | |
this.level = level; | |
this.reinit(); | |
}; | |
log.Logger.prototype.reinit = function () { | |
for (var i = 0; i < levels.length; i++) { | |
var d = levels[i]; | |
this[d.name] = (d.obj.level <= this.level.level) | |
? bind(d.type, '[' + this.name + ':' + d.obj.name + ']') | |
: function () {}; | |
} | |
}; | |
return log; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment