2014-02-28 04:39:31 +08:00
|
|
|
"use strict";
|
|
|
|
|
2015-07-14 04:55:42 +08:00
|
|
|
/* eslint no-use-before-define: 0 */
|
2014-10-25 05:54:56 +08:00
|
|
|
|
2015-07-14 04:55:42 +08:00
|
|
|
var Config = require("./config"),
|
2015-02-21 10:49:31 +08:00
|
|
|
_ = require("./utils/helpers");
|
2014-05-14 10:05:01 +08:00
|
|
|
|
2015-07-14 04:55:42 +08:00
|
|
|
var BasicLogger = function basiclogger(str) {
|
|
|
|
var prefix = new Date().toISOString() + " : ";
|
|
|
|
console.log(prefix + str);
|
|
|
|
};
|
|
|
|
|
|
|
|
var NullLogger = function nulllogger() {};
|
|
|
|
|
2015-01-14 02:43:20 +08:00
|
|
|
var Logger = module.exports = {
|
2015-07-14 04:55:42 +08:00
|
|
|
setup: setup,
|
2014-05-14 10:05:01 +08:00
|
|
|
|
2015-07-14 04:55:42 +08:00
|
|
|
should: {
|
|
|
|
log: true,
|
|
|
|
debug: false
|
|
|
|
},
|
2014-12-16 03:15:29 +08:00
|
|
|
|
2015-07-14 04:55:42 +08:00
|
|
|
log: function log(str) {
|
|
|
|
if (Logger.should.log) {
|
|
|
|
Logger.logger.call(Logger.logger, str);
|
2015-03-23 08:07:25 +08:00
|
|
|
}
|
2015-07-14 04:55:42 +08:00
|
|
|
},
|
2015-03-23 08:07:25 +08:00
|
|
|
|
2015-07-14 04:55:42 +08:00
|
|
|
debug: function debug(str) {
|
|
|
|
if (Logger.should.log && Logger.should.debug) {
|
|
|
|
Logger.logger.call(Logger.logger, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-12-16 03:15:29 +08:00
|
|
|
|
2015-07-14 04:55:42 +08:00
|
|
|
function setup(opts) {
|
|
|
|
if (_.isObject(opts)) { _.extend(Config, opts); }
|
2014-12-16 03:15:29 +08:00
|
|
|
|
2015-07-14 04:55:42 +08:00
|
|
|
var logger = Config.logger;
|
|
|
|
|
|
|
|
// if no logger supplied, use basic logger
|
|
|
|
if (logger == null) { logger = BasicLogger; }
|
|
|
|
|
|
|
|
// if logger is still falsy, use NullLogger
|
|
|
|
Logger.logger = logger || NullLogger;
|
2014-11-29 01:37:44 +08:00
|
|
|
|
2015-07-14 04:55:42 +08:00
|
|
|
Logger.should.log = !Config.silent;
|
|
|
|
Logger.should.debug = Config.debug;
|
|
|
|
|
|
|
|
// --silent CLI flag overrides
|
|
|
|
if (_.includes(process.argv, "--silent")) {
|
|
|
|
Logger.should.log = false;
|
2014-11-29 01:37:44 +08:00
|
|
|
}
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2015-07-14 04:55:42 +08:00
|
|
|
// --debug CLI flag overrides
|
|
|
|
if (_.includes(process.argv, "--debug")) {
|
|
|
|
Logger.should.debug = false;
|
|
|
|
}
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2015-07-14 04:55:42 +08:00
|
|
|
return Logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
setup();
|
|
|
|
Config.subscribe(setup);
|
|
|
|
|
|
|
|
// deprecated holdovers
|
|
|
|
["info", "warn", "error", "fatal"].forEach(function(method) {
|
|
|
|
var called = false;
|
|
|
|
|
|
|
|
function showDeprecationNotice() {
|
|
|
|
console.log("The method Logger#" + method + " has been deprecated.");
|
|
|
|
console.log("It will be removed in Cylon 2.0.0.");
|
|
|
|
console.log("Please switch to using the #log or #debug Logger methods");
|
|
|
|
|
|
|
|
called = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger[method] = function() {
|
|
|
|
if (!called) { showDeprecationNotice(); }
|
|
|
|
Logger.log.apply(null, arguments);
|
2014-10-27 23:59:36 +08:00
|
|
|
};
|
2014-10-25 05:54:56 +08:00
|
|
|
});
|