cylon/lib/logger.js

49 lines
1.0 KiB
JavaScript
Raw Normal View History

2013-10-25 05:25:42 +08:00
/*
* logger
* 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";
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"),
_ = require("./lodash");
2014-05-14 10:05:01 +08:00
2015-01-14 02:43:20 +08:00
var Logger = module.exports = {
setup: function(opts) {
if (typeof opts === "object") {
_.merge(Config.logging, opts);
}
2014-05-14 10:05:01 +08:00
2015-01-14 02:43:20 +08:00
var logger = Config.logging.logger,
level = Config.logging.level || "info";
2014-12-16 03:15:29 +08:00
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;
},
2015-01-14 02:43:20 +08:00
toString: function() {
return this.logger.toString();
}
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-01-14 02:43:20 +08:00
_.each(levels, function(level) {
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
};
});