cylon/lib/logger.js

83 lines
1.8 KiB
JavaScript
Raw Normal View History

2014-02-28 04:39:31 +08:00
"use strict";
/* eslint no-use-before-define: 0 */
var Config = require("./config"),
_ = require("./utils/helpers");
2014-05-14 10:05:01 +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 = {
setup: setup,
2014-05-14 10:05:01 +08:00
should: {
log: true,
debug: false
},
2014-12-16 03:15:29 +08:00
log: function log(str) {
if (Logger.should.log) {
Logger.logger.call(Logger.logger, str);
}
},
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
function setup(opts) {
if (_.isObject(opts)) { _.extend(Config, opts); }
2014-12-16 03:15:29 +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;
Logger.should.log = !Config.silent;
Logger.should.debug = Config.debug;
// --silent CLI flag overrides
if (_.includes(process.argv, "--silent")) {
Logger.should.log = false;
}
2013-10-25 05:25:42 +08:00
// --debug CLI flag overrides
if (_.includes(process.argv, "--debug")) {
Logger.should.debug = false;
}
2013-10-25 05:25: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
};
});