Refactor logger.js

This commit is contained in:
Andrew Stewart 2014-02-27 12:39:31 -08:00
parent 82effb6b64
commit 599a5bb288
1 changed files with 104 additions and 86 deletions

View File

@ -6,108 +6,126 @@
* Licensed under the Apache 2.0 license.
*/
"use strict";
(function() {
var BasicLogger, NullLogger,
__slice = [].slice;
var getArgs = function(args) {
return args.length >= 1 ? [].slice.call(args, 0) : [];
}
global.Logger = {
setup: function(logger) {
if (logger == null) {
logger = new BasicLogger;
}
return this.logger = logger === false ? new NullLogger : logger;
},
toString: function() {
return this.logger.toString();
},
debug: function() {
var args, _ref;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return (_ref = this.logger).debug.apply(_ref, args);
},
info: function() {
var args, _ref;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return (_ref = this.logger).info.apply(_ref, args);
},
warn: function() {
var args, _ref;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return (_ref = this.logger).warn.apply(_ref, args);
},
error: function() {
var args, _ref;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return (_ref = this.logger).error.apply(_ref, args);
},
fatal: function() {
var args, _ref;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return (_ref = this.logger).fatal.apply(_ref, args);
// The Logger is a global object to facilitate logging stuff to the console (or
// other output) easily and consistently. It's available anywhere in Cylon, as
// well as in external modules that are loaded into Cylon
global.Logger = {
// Public: Creates a Logger instance and assigns it to @logger
//
// logger - logger object to use. Defaults to a BasicLogger, or a NullLogger if
// false is supplied
//
// Returns the new logger instance
setup: function(logger) {
if (logger == null) { logger = new BasicLogger; }
if (logger === false) {
this.logger = new NullLogger();
} else {
this.logger = logger;
}
return this.logger
},
toString: function() { return this.logger.toString(); },
debug: function() {
var args = getArgs(arguments);
return this.logger.debug.apply(this.logger, args);
},
info: function() {
var args = getArgs(arguments);
return this.logger.info.apply(this.logger, args);
},
warn: function() {
var args = getArgs(arguments);
return this.logger.warn.apply(this.logger, args);
},
error: function() {
var args = getArgs(arguments);
return this.logger.error.apply(this.logger, args);
},
fatal: function() {
var args = getArgs(arguments);
return this.logger.fatal.apply(this.logger, args);
}
};
// The BasicLogger pushes stuff to console.log. Nothing more, nothing less.
var BasicLogger = (function() {
function BasicLogger() {}
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 = (function() {
function BasicLogger() {}
BasicLogger.prototype.info = function() {
var args = getArgs(arguments),
string = ["I, [" + (new Date().toISOString()) + "] INFO -- :"],
data = string.concat(args.slice());
BasicLogger.prototype.toString = function() {
return "BasicLogger";
};
return console.log.apply(console, data);
};
BasicLogger.prototype.debug = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return console.log.apply(console, ["D, [" + (new Date().toISOString()) + "] DEBUG -- :"].concat(__slice.call(args)));
};
BasicLogger.prototype.warn = function() {
var args = getArgs(arguments),
string = ["W, [" + (new Date().toISOString()) + "] WARN -- :"],
data = string.concat(args.slice());
BasicLogger.prototype.info = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return console.log.apply(console, ["I, [" + (new Date().toISOString()) + "] INFO -- :"].concat(__slice.call(args)));
};
return console.log.apply(console, data);
};
BasicLogger.prototype.warn = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return console.log.apply(console, ["W, [" + (new Date().toISOString()) + "] WARN -- :"].concat(__slice.call(args)));
};
BasicLogger.prototype.error = function() {
var args = getArgs(arguments),
string = ["E, [" + (new Date().toISOString()) + "] ERROR -- :"],
data = string.concat(args.slice());
BasicLogger.prototype.error = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return console.log.apply(console, ["E, [" + (new Date().toISOString()) + "] ERROR -- :"].concat(__slice.call(args)));
};
return console.log.apply(console, data);
};
BasicLogger.prototype.fatal = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return console.log.apply(console, ["F, [" + (new Date().toISOString()) + "] FATAL -- :"].concat(__slice.call(args)));
};
BasicLogger.prototype.fatal = function() {
var args = getArgs(arguments),
string = ["F, [" + (new Date().toISOString()) + "] FATAL -- :"],
data = string.concat(args.slice());
return BasicLogger;
return console.log.apply(console, data);
};
})();
return BasicLogger;
NullLogger = (function() {
function NullLogger() {}
})();
NullLogger.prototype.toString = function() {
return "NullLogger";
};
// The NullLogger is designed for cases where you want absolutely nothing to
// print to anywhere. Every proxied method from the Logger returns a noop.
var NullLogger = (function() {
function NullLogger() {}
NullLogger.prototype.debug = function() {};
NullLogger.prototype.toString = function() { return "NullLogger"; };
NullLogger.prototype.info = function() {};
NullLogger.prototype.debug = function() {};
NullLogger.prototype.info = function() {};
NullLogger.prototype.warn = function() {};
NullLogger.prototype.error = function() {};
NullLogger.prototype.fatal = function() {};
NullLogger.prototype.warn = function() {};
return NullLogger;
NullLogger.prototype.error = function() {};
NullLogger.prototype.fatal = function() {};
return NullLogger;
})();
}).call(this);
})();