Generate BasicLogger methods, rather than duplicating code

This commit is contained in:
Andrew Stewart 2014-06-06 17:09:22 -07:00
parent 08c6b6e3dc
commit b1d7dd8186
1 changed files with 17 additions and 40 deletions

View File

@ -5,46 +5,23 @@ BasicLogger.prototype.toString = function() {
return "BasicLogger";
};
BasicLogger.prototype.debug = function() {
var args = getArgs(arguments),
string = ["D, [" + (new Date().toISOString()) + "] DEBUG -- :"],
data = string.concat(args.slice());
return console.log.apply(console, data);
};
BasicLogger.prototype.info = function() {
var args = getArgs(arguments),
string = ["I, [" + (new Date().toISOString()) + "] INFO -- :"],
data = string.concat(args.slice());
return console.log.apply(console, data);
};
BasicLogger.prototype.warn = function() {
var args = getArgs(arguments),
string = ["W, [" + (new Date().toISOString()) + "] WARN -- :"],
data = string.concat(args.slice());
return console.log.apply(console, data);
};
BasicLogger.prototype.error = function() {
var args = getArgs(arguments),
string = ["E, [" + (new Date().toISOString()) + "] ERROR -- :"],
data = string.concat(args.slice());
return console.log.apply(console, data);
};
BasicLogger.prototype.fatal = function() {
var args = getArgs(arguments),
string = ["F, [" + (new Date().toISOString()) + "] FATAL -- :"],
data = string.concat(args.slice());
return console.log.apply(console, data);
};
var getArgs = function(args) {
return args.length >= 1 ? [].slice.call(args, 0) : [];
};
var logString = function(type) {
var upcase = new String(type).toUpperCase(),
time = new Date().toISOString();
var padded = new String(" " + upcase).slice(-5);
return upcase[0] + ", [" + time + "] " + padded + " -- :";
};
['debug', 'info', 'warn', 'error', 'fatal'].forEach(function(type) {
BasicLogger.prototype[type] = function() {
var args = getArgs(arguments);
return console.log.apply(console, [].concat(logString(type), args));
};
});