Clearer iteration through keys

- We needed to closure the variable `levels` and this is cleaner
This commit is contained in:
Esteban Ordano 2014-09-01 17:43:02 -03:00
parent 6520f8c5e6
commit 4415f1c862
1 changed files with 16 additions and 18 deletions

View File

@ -14,26 +14,24 @@ var levels = {
'fatal': 5
};
for (_level in levels) {
(function(level) {
Logger.prototype[level] = function() {
if (levels[level] >= levels[this.level]) {
var str = '[' + level + '] ' + this.name + ': ' + arguments[0],
extraArgs,
extraArgs = [].slice.call(arguments, 1);
if (console[level]) {
extraArgs.unshift(str);
console[level].apply(console, extraArgs);
} else {
if (extraArgs.length) {
str += JSON.stringify(extraArgs);
}
console.log(str);
Object.keys(levels).forEach(function(level) {
Logger.prototype[level] = function() {
if (levels[level] >= levels[this.level]) {
var str = '[' + level + '] ' + this.name + ': ' + arguments[0],
extraArgs,
extraArgs = [].slice.call(arguments, 1);
if (console[level]) {
extraArgs.unshift(str);
console[level].apply(console, extraArgs);
} else {
if (extraArgs.length) {
str += JSON.stringify(extraArgs);
}
console.log(str);
}
};
})(_level);
}
}
};
});
Logger.prototype.setLevel = function(level) {
this.level = level;