UNPKG

@backstage/backend-app-api

Version:

Core API used by Backstage backend apps

79 lines (76 loc) 2.74 kB
'use strict'; const LOGGER_INTERVAL_MAX = 6e4; function joinIds(ids) { return [...ids].map((id) => `'${id}'`).join(", "); } function createInitializationLogger(pluginIds, rootLogger) { const logger = rootLogger?.child({ type: "initialization" }); const starting = new Set(pluginIds); const started = /* @__PURE__ */ new Set(); logger?.info(`Plugin initialization started: ${joinIds(pluginIds)}`); const getInitStatus = () => { let status = ""; if (started.size > 0) { status = `, newly initialized: ${joinIds(started)}`; started.clear(); } if (starting.size > 0) { status += `, still initializing: ${joinIds(starting)}`; } return status; }; let interval = 1e3; let prevInterval = 0; let timeout; const onTimeout = () => { logger?.info(`Plugin initialization in progress${getInitStatus()}`); const nextInterval = Math.min(interval + prevInterval, LOGGER_INTERVAL_MAX); prevInterval = interval; interval = nextInterval; timeout = setTimeout(onTimeout, nextInterval); }; timeout = setTimeout(onTimeout, interval); return { onPluginStarted(pluginId) { starting.delete(pluginId); started.add(pluginId); }, onPluginFailed(pluginId, error) { starting.delete(pluginId); const status = starting.size > 0 ? `, waiting for ${starting.size} other plugins to finish before shutting down the process` : ""; logger?.error( `Plugin '${pluginId}' threw an error during startup${status}.`, error ); }, onPermittedPluginFailure(pluginId, error) { starting.delete(pluginId); logger?.error( `Plugin '${pluginId}' threw an error during startup, but boot failure is permitted for this plugin so startup will continue.`, error ); }, onPluginModuleFailed(pluginId, moduleId, error) { const status = starting.size > 0 ? `, waiting for ${starting.size} other plugins to finish before shutting down the process` : ""; logger?.error( `Module ${moduleId} in Plugin '${pluginId}' threw an error during startup${status}.`, error ); }, onPermittedPluginModuleFailure(pluginId, moduleId, error) { logger?.error( `Module ${moduleId} in Plugin '${pluginId}' threw an error during startup, but boot failure is permitted for this plugin module so startup will continue.`, error ); }, onAllStarted() { logger?.info(`Plugin initialization complete${getInitStatus()}`); if (timeout) { clearTimeout(timeout); timeout = void 0; } } }; } exports.createInitializationLogger = createInitializationLogger; //# sourceMappingURL=createInitializationLogger.cjs.js.map