typescript-logging
Version:
Library for logging, written in typescript, can be used by normal es5+ javascript as well.
171 lines • 7.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var DataStructures_1 = require("../../utils/DataStructures");
var LoggerOptions_1 = require("../LoggerOptions");
var LoggerFactoryImpl_1 = require("./LoggerFactoryImpl");
var ExtensionHelper_1 = require("../../extension/ExtensionHelper");
var LogGroupRule_1 = require("./LogGroupRule");
var LoggerFactoryOptions_1 = require("./LoggerFactoryOptions");
var LFServiceImpl = (function () {
function LFServiceImpl() {
// Private constructor.
this._nameCounter = 1;
this._mapFactories = new DataStructures_1.SimpleMap();
ExtensionHelper_1.ExtensionHelper.register();
}
LFServiceImpl.getInstance = function () {
// Loaded on demand. Do NOT change as webpack may pack things in wrong order otherwise.
if (LFServiceImpl._INSTANCE === null) {
LFServiceImpl._INSTANCE = new LFServiceImpl();
}
return LFServiceImpl._INSTANCE;
};
/**
* Create a new LoggerFactory with given options (if any). If no options
* are specified, the LoggerFactory, will accept any named logger and will
* log on info level by default for, to the console.
* @param options Options, optional.
* @returns {LoggerFactory}
*/
LFServiceImpl.prototype.createLoggerFactory = function (options) {
if (options === void 0) { options = null; }
var name = "LoggerFactory" + this._nameCounter++;
return this.createNamedLoggerFactory(name, options);
};
/**
* Create a new LoggerFactory using given name (used for console api/extension).
* @param name Name Pick something short but distinguishable.
* @param options Options, optional
* @return {LoggerFactory}
*/
LFServiceImpl.prototype.createNamedLoggerFactory = function (name, options) {
if (options === void 0) { options = null; }
if (this._mapFactories.exists(name)) {
throw new Error("LoggerFactory with name " + name + " already exists.");
}
var factory;
if (options !== null) {
factory = new LoggerFactoryImpl_1.LoggerFactoryImpl(name, options);
}
else {
factory = new LoggerFactoryImpl_1.LoggerFactoryImpl(name, LFServiceImpl.createDefaultOptions());
}
this._mapFactories.put(name, factory);
return factory;
};
/**
* Closes all Loggers for LoggerFactories that were created.
* After this call, all previously fetched Loggers (from their
* factories) are unusable. The factories remain as they were.
*/
LFServiceImpl.prototype.closeLoggers = function () {
this._mapFactories.values().forEach(function (factory) {
factory.closeLoggers();
});
this._mapFactories.clear();
this._nameCounter = 1;
};
LFServiceImpl.prototype.getRuntimeSettingsForLoggerFactories = function () {
var result = [];
this._mapFactories.forEachValue(function (factory) { return result.push(factory); });
return result;
};
LFServiceImpl.prototype.getLogGroupSettings = function (nameLoggerFactory, idLogGroupRule) {
var factory = this._mapFactories.get(nameLoggerFactory);
if (typeof factory === "undefined") {
return null;
}
return factory.getLogGroupRuntimeSettingsByIndex(idLogGroupRule);
};
LFServiceImpl.prototype.getLoggerFactoryRuntimeSettingsByName = function (nameLoggerFactory) {
var result = this._mapFactories.get(nameLoggerFactory);
if (typeof result === "undefined") {
return null;
}
return result;
};
LFServiceImpl.createDefaultOptions = function () {
return new LoggerFactoryOptions_1.LoggerFactoryOptions().addLogGroupRule(new LogGroupRule_1.LogGroupRule(new RegExp(".+"), LoggerOptions_1.LogLevel.Info));
};
// Loaded on demand. Do NOT change as webpack may pack things in wrong order otherwise.
LFServiceImpl._INSTANCE = null;
return LFServiceImpl;
}());
/**
* Create and configure your LoggerFactory from here.
*/
var LFService = (function () {
function LFService() {
}
/**
* Create a new LoggerFactory with given options (if any). If no options
* are specified, the LoggerFactory, will accept any named logger and will
* log on info level by default for, to the console.
* @param options Options, optional.
* @returns {LoggerFactory}
*/
LFService.createLoggerFactory = function (options) {
if (options === void 0) { options = null; }
return LFService.INSTANCE_SERVICE.createLoggerFactory(options);
};
/**
* Create a new LoggerFactory using given name (used for console api/extension).
* @param name Name Pick something short but distinguishable. The word "DEFAULT" is reserved and cannot be taken, it is used
* for the default LoggerFactory.
* @param options Options, optional
* @return {LoggerFactory}
*/
LFService.createNamedLoggerFactory = function (name, options) {
if (options === void 0) { options = null; }
if (name === LFService.DEFAULT_LOGGER_FACTORY_NAME) {
throw new Error("LoggerFactory name: " + LFService.DEFAULT_LOGGER_FACTORY_NAME + " is reserved and cannot be used.");
}
return LFService.INSTANCE_SERVICE.createNamedLoggerFactory(name, options);
};
/**
* Closes all Loggers for LoggerFactories that were created.
* After this call, all previously fetched Loggers (from their
* factories) are unusable. The factories remain as they were.
*/
LFService.closeLoggers = function () {
return LFService.INSTANCE_SERVICE.closeLoggers();
};
/**
* Return LFServiceRuntimeSettings to retrieve information loggerfactories
* and their runtime settings.
* @returns {LFServiceRuntimeSettings}
*/
LFService.getRuntimeSettings = function () {
return LFService.INSTANCE_SERVICE;
};
Object.defineProperty(LFService, "DEFAULT", {
/**
* This property returns the default LoggerFactory (if not yet initialized it is initialized).
* This LoggerFactory can be used to share among multiple
* applications/libraries - that way you can enable/change logging over everything from
* your own application when required.
* It is recommended to be used by library developers to make logging easily available for the
* consumers of their libraries.
* It is highly recommended to use Loggers from the LoggerFactory with unique grouping/names to prevent
* clashes of Loggers between multiple projects.
* @returns {LoggerFactory} Returns the default LoggerFactory
*/
get: function () {
return LFService.getDefault();
},
enumerable: true,
configurable: true
});
LFService.getDefault = function () {
if (LFService.DEFAULT_LOGGER_FACTORY === null) {
LFService.DEFAULT_LOGGER_FACTORY = LFService.DEFAULT_LOGGER_FACTORY = LFService.INSTANCE_SERVICE.createNamedLoggerFactory(LFService.DEFAULT_LOGGER_FACTORY_NAME, new LoggerFactoryOptions_1.LoggerFactoryOptions().addLogGroupRule(new LogGroupRule_1.LogGroupRule(new RegExp(".+"), LoggerOptions_1.LogLevel.Error)));
}
return LFService.DEFAULT_LOGGER_FACTORY;
};
LFService.DEFAULT_LOGGER_FACTORY_NAME = "DEFAULT";
LFService.INSTANCE_SERVICE = LFServiceImpl.getInstance();
LFService.DEFAULT_LOGGER_FACTORY = null;
return LFService;
}());
exports.LFService = LFService;
//# sourceMappingURL=LFService.js.map