2015-04-15 09:16:03 +08:00
|
|
|
/**
|
|
|
|
* Cylon.js - Logger
|
2013-10-25 05:25:42 +08:00
|
|
|
* cylonjs.com
|
|
|
|
*
|
2015-01-08 04:58:50 +08:00
|
|
|
* Copyright (c) 2013-2015 The Hybrid Group
|
2013-10-25 05:25:42 +08:00
|
|
|
* Licensed under the Apache 2.0 license.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 04:39:31 +08:00
|
|
|
"use strict";
|
|
|
|
|
2014-10-25 05:54:56 +08:00
|
|
|
var levels = ["debug", "info", "warn", "error", "fatal"];
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
var BasicLogger = require("./logger/basic_logger"),
|
|
|
|
NullLogger = require("./logger/null_logger"),
|
2015-01-14 02:43:20 +08:00
|
|
|
Config = require("./config"),
|
2015-02-21 10:49:31 +08:00
|
|
|
_ = require("./utils/helpers");
|
2014-05-14 10:05:01 +08:00
|
|
|
|
2015-01-14 02:43:20 +08:00
|
|
|
var Logger = module.exports = {
|
|
|
|
setup: function(opts) {
|
2015-02-21 10:49:31 +08:00
|
|
|
if (_.isObject(opts)) {
|
|
|
|
Config.logging = _.extend(Config.logging, opts);
|
2015-01-14 02:43:20 +08:00
|
|
|
}
|
2014-05-14 10:05:01 +08:00
|
|
|
|
2015-01-14 02:43:20 +08:00
|
|
|
var logger = Config.logging.logger,
|
2015-04-15 12:49:12 +08:00
|
|
|
level = Config.logging.level || "info";
|
2014-12-16 03:15:29 +08:00
|
|
|
|
2015-03-23 08:07:25 +08:00
|
|
|
// --debug CLI flag overrides any other option
|
|
|
|
if (_.includes(process.argv, "--debug")) {
|
|
|
|
level = "debug";
|
|
|
|
}
|
|
|
|
|
2015-01-14 02:43:20 +08:00
|
|
|
logger = (logger == null) ? BasicLogger : logger;
|
2014-12-16 03:15:29 +08:00
|
|
|
|
2015-01-14 02:43:20 +08:00
|
|
|
this.logger = logger || NullLogger;
|
|
|
|
this.level = level;
|
2014-12-16 03:15:29 +08:00
|
|
|
|
2015-01-14 02:43:20 +08:00
|
|
|
return this;
|
|
|
|
},
|
2014-11-29 01:37:44 +08:00
|
|
|
|
2015-01-14 02:43:20 +08:00
|
|
|
toString: function() {
|
|
|
|
return this.logger.toString();
|
2014-11-29 01:37:44 +08:00
|
|
|
}
|
2014-05-14 10:05:01 +08:00
|
|
|
};
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2015-01-14 02:43:20 +08:00
|
|
|
Logger.setup();
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
levels.forEach(function(level) {
|
2014-10-25 05:54:56 +08:00
|
|
|
Logger[level] = function() {
|
2015-01-14 02:43:20 +08:00
|
|
|
if (levels.indexOf(level) >= levels.indexOf(Logger.level)) {
|
|
|
|
return Logger.logger[level].apply(Logger.logger, arguments);
|
|
|
|
}
|
2014-10-27 23:59:36 +08:00
|
|
|
};
|
2014-10-25 05:54:56 +08:00
|
|
|
});
|