UNPKG

slf4ts-api

Version:

Simple logging facade for nodejs

172 lines 7.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("source-map-support/register"); const LoggerBindings_1 = require("./LoggerBindings"); const LoggerConfiguration_1 = require("./LoggerConfiguration"); class DefaultLoggerInstance { constructor(name, group, logLevel, commonMetadata, impl, builder) { this.impl = impl; this.name = name; this.group = group; this.logLevel = logLevel; this.commonMetadata = commonMetadata; const initialConfig = LoggerConfiguration_1.LoggerConfiguration.getConfig(group, name); this.impl.setLoggerBuilder(builder); this.impl.setConfig(initialConfig, group, name); } getLogLevel() { return this.logLevel; } setLogLevel(logLevel) { this.logLevel = logLevel; this.impl.setLogLevel(logLevel, this.group, this.name); } getName() { return this.name; } getGroup() { return this.group; } getImpl() { return this.impl; } getMetadata() { return this.commonMetadata; } setMetadata(commonMetadata) { this.commonMetadata = commonMetadata; this.impl.setMetadata(commonMetadata, this.group, this.name); } async trace(...args) { const metadata = [LoggerConfiguration_1.LogLevel.TRACE, this.group, this.name].concat(...arguments).concat(this.commonMetadata); return this.log.apply(this, metadata); } async debug(...args) { const metadata = [LoggerConfiguration_1.LogLevel.DEBUG, this.group, this.name].concat(...arguments).concat(this.commonMetadata); return this.log.apply(this, metadata); } async info(...args) { const metadata = [LoggerConfiguration_1.LogLevel.INFO, this.group, this.name].concat(...arguments).concat(this.commonMetadata); return this.log.apply(this, metadata); } async warn(...args) { const metadata = [LoggerConfiguration_1.LogLevel.WARN, this.group, this.name].concat(...arguments).concat(this.commonMetadata); return this.log.apply(this, metadata); } async error(...args) { const metadata = [LoggerConfiguration_1.LogLevel.ERROR, this.group, this.name].concat(...arguments).concat(this.commonMetadata); return this.log.apply(this, metadata); } getImplementation() { return this.impl.getImplementation(this.group, this.name); } async log(logLevel, ...args) { if (logLevel <= this.logLevel) { return this.impl.log.apply(this.impl, arguments); } return Promise.resolve(); } } exports.DefaultLoggerInstance = DefaultLoggerInstance; class NullLoggerImplementation { async log(...args) { return null; } getImplementation(group, name) { return null; } setConfig(config, group, name) { } setLogLevel(logLevel, group, name) { } setMetadata(metadata, group, name) { } setLoggerBuilder(builder) { } } class LoggerFactory { static getLogger(group = '', name = '', builder) { if (!LoggerFactory.INITIALIZED) { LoggerFactory.INITIALIZED = true; LoggerFactory.initialize(); } const compoundKey = `${group}:${name}`; if (LoggerFactory.LOGGER_INSTANCE_CACHE.has(compoundKey)) { return LoggerFactory.LOGGER_INSTANCE_CACHE.get(compoundKey); } const instance = new DefaultLoggerInstance(name, group, LoggerConfiguration_1.LoggerConfiguration.getLogLevel(group, name), LoggerFactory.COMMON_METADATA, LoggerFactory.LOGGER, builder); LoggerFactory.LOGGER_INSTANCE_CACHE.set(compoundKey, instance); return instance; } static reset(reinit = false) { LoggerFactory.LOGGER_INSTANCE_CACHE.clear(); if (reinit) { LoggerFactory.INITIALIZED = false; } } static getMetadata() { return LoggerFactory.COMMON_METADATA; } static setMetadata(metadata) { const formerData = LoggerFactory.COMMON_METADATA; LoggerFactory.COMMON_METADATA = metadata; LoggerFactory.LOGGER_INSTANCE_CACHE.forEach((logger, compoundKey) => { if (logger.getMetadata() === formerData) { logger.setMetadata(LoggerFactory.COMMON_METADATA); } }); } static initialize() { const BINDINGS = new LoggerBindings_1.LoggerBindings().getBindings(); const BINDING = BINDINGS[0]; if (BINDINGS.length === 0) { console.log('SLF4TS: No Logger Binding found'); return; } LoggerFactory.LOGGER = BINDING.getLoggerImplementation(); LoggerFactory.ROOT_LOGGER = LoggerFactory.getLogger(); if (BINDINGS.length > 1) { let message = 'multiple bindings found:'; BINDINGS.forEach((binding) => { message += `\n ${binding.getVendor()} - ${binding.getVersion()}`; }); message += `\n using ${BINDING.getVendor()} - ${BINDING.getVersion()}`; LoggerFactory.ROOT_LOGGER.info(message) .catch(console.error); } LoggerConfiguration_1.LoggerConfiguration.onLogLevelChanged(LoggerFactory.logLevelChanged); LoggerConfiguration_1.LoggerConfiguration.onConfigChanged(LoggerFactory.configChanged); } static logLevelChanged(event) { const groupEmpty = event.group === ''; const nameEmpty = event.name === ''; LoggerFactory.LOGGER_INSTANCE_CACHE.forEach((logger, compoundKey) => { const groupMatches = logger.getGroup() === event.group; const nameMatches = logger.getName() === event.name; if ((groupEmpty && nameEmpty) || (groupMatches && nameEmpty) || (groupMatches && nameMatches)) { logger.setLogLevel(event.logLevel); } }); } static configChanged(event) { const groupEmpty = event.group === ''; const nameEmpty = event.name === ''; LoggerFactory.LOGGER_INSTANCE_CACHE.forEach((logger, compoundKey) => { const hasNameConfig = LoggerConfiguration_1.LoggerConfiguration.hasConfig(logger.getGroup(), logger.getName()); const hasGroupConfig = LoggerConfiguration_1.LoggerConfiguration.hasConfig(logger.getGroup()); const groupMatches = logger.getGroup() === event.group; const nameMatches = logger.getName() === event.name; if ((groupEmpty && nameEmpty && !hasGroupConfig && !hasNameConfig) || (nameEmpty && groupMatches && !hasNameConfig) || (groupMatches && nameMatches)) { logger.getImpl().setConfig(event.config, logger.getGroup(), logger.getName()); } }); } } exports.LoggerFactory = LoggerFactory; LoggerFactory.COMMON_METADATA = undefined; LoggerFactory.LOGGER = new NullLoggerImplementation(); LoggerFactory.INITIALIZED = false; LoggerFactory.LOGGER_INSTANCE_CACHE = new Map(); //# sourceMappingURL=LoggerFactory.js.map