UNPKG

@vulcan-sql/core

Version:
86 lines 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.asyncReqIdStorage = exports.getLogger = exports.LoggingLevel = exports.LoggingScope = exports.ILogger = void 0; const tslog_1 = require("tslog"); Object.defineProperty(exports, "ILogger", { enumerable: true, get: function () { return tslog_1.Logger; } }); const async_hooks_1 = require("async_hooks"); const errors_1 = require("./errors"); // The category according to package name var LoggingScope; (function (LoggingScope) { LoggingScope["CORE"] = "CORE"; LoggingScope["BUILD"] = "BUILD"; LoggingScope["SERVE"] = "SERVE"; LoggingScope["ACCESS_LOG"] = "ACCESS_LOG"; })(LoggingScope = exports.LoggingScope || (exports.LoggingScope = {})); var LoggingLevel; (function (LoggingLevel) { LoggingLevel["SILLY"] = "silly"; LoggingLevel["TRACE"] = "trace"; LoggingLevel["DEBUG"] = "debug"; LoggingLevel["INFO"] = "info"; LoggingLevel["WARN"] = "warn"; LoggingLevel["ERROR"] = "error"; LoggingLevel["FATAL"] = "fatal"; })(LoggingLevel = exports.LoggingLevel || (exports.LoggingLevel = {})); // The default logger options const defaultLoggerOptions = { level: LoggingLevel.DEBUG, displayRequestId: false, displayFilePath: 'hidden', displayFunctionName: false, }; class LoggerFactory { constructor() { this.asyncReqIdStorage = new async_hooks_1.AsyncLocalStorage(); this.loggerMap = { // Here, create default scope logger, we could add other package or extension logger name in here [LoggingScope.CORE]: this.createLogger(LoggingScope.CORE), [LoggingScope.BUILD]: this.createLogger(LoggingScope.BUILD), [LoggingScope.SERVE]: this.createLogger(LoggingScope.SERVE), [LoggingScope.ACCESS_LOG]: this.createLogger(LoggingScope.ACCESS_LOG), }; } getLogger({ scopeName, options, }) { if (!(scopeName in LoggingScope)) throw new errors_1.InternalError(`The ${scopeName} does not belong to ${Object.keys(LoggingScope)}`); // if scope name exist in mapper and not update config if (scopeName in this.loggerMap) { if (!options) return this.loggerMap[scopeName]; // if options existed, update settings. const logger = this.loggerMap[scopeName]; this.updateSettings(logger, options); return logger; } // if scope name does not exist in map or exist but would like to update config const newLogger = this.createLogger(scopeName, options); this.loggerMap[scopeName] = newLogger; return newLogger; } updateSettings(logger, options) { const prevSettings = logger.settings; logger.setSettings({ minLevel: options.level || prevSettings.minLevel, displayRequestId: options.displayRequestId || prevSettings.displayRequestId, displayFunctionName: options.displayFunctionName || prevSettings.displayFunctionName, displayFilePath: options.displayFilePath || prevSettings.displayFilePath, }); } createLogger(name, options) { return new tslog_1.Logger({ name, minLevel: (options === null || options === void 0 ? void 0 : options.level) || defaultLoggerOptions.level, // use function call for requestId, then when logger get requestId, it will get newest store again requestId: () => { var _a; return (_a = this.asyncReqIdStorage.getStore()) === null || _a === void 0 ? void 0 : _a.requestId; }, displayRequestId: (options === null || options === void 0 ? void 0 : options.displayRequestId) || defaultLoggerOptions.displayRequestId, displayFunctionName: (options === null || options === void 0 ? void 0 : options.displayFunctionName) || defaultLoggerOptions.displayFunctionName, displayFilePath: (options === null || options === void 0 ? void 0 : options.displayFilePath) || defaultLoggerOptions.displayFilePath, }); } } const factory = new LoggerFactory(); exports.getLogger = factory.getLogger.bind(factory); exports.asyncReqIdStorage = factory.asyncReqIdStorage; //# sourceMappingURL=logger.js.map