UNPKG

nestjs-temporal-core

Version:

Complete NestJS integration for Temporal.io with auto-discovery, declarative scheduling, enhanced monitoring, and enterprise-ready features

179 lines 6.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LoggerUtils = exports.TemporalLogger = exports.TemporalLoggerManager = void 0; exports.createLogger = createLogger; const common_1 = require("@nestjs/common"); class TemporalLoggerManager { constructor() { this.globalConfig = TemporalLoggerManager.DEFAULT_CONFIG; this.loggerCache = new Map(); } static getInstance() { return (TemporalLoggerManager.instance ?? (TemporalLoggerManager.instance = new TemporalLoggerManager())); } configure(config) { if (!config || typeof config !== 'object') { return; } const hasChanges = Object.keys(config).some((key) => this.globalConfig[key] !== config[key]); if (hasChanges) { this.globalConfig = { ...this.globalConfig, ...config }; this.loggerCache.clear(); } } getGlobalConfig() { return this.globalConfig; } createLogger(context, localConfig = {}) { const cacheKey = this.getCacheKey(context, localConfig); if (this.loggerCache.has(cacheKey)) { return this.loggerCache.get(cacheKey); } const mergedConfig = { ...this.globalConfig, ...localConfig }; const logger = new TemporalLogger(context, mergedConfig); this.loggerCache.set(cacheKey, logger); return logger; } getCacheKey(context, localConfig) { return `${context}:${localConfig?.enableLogger ?? ''}:${localConfig?.logLevel ?? ''}`; } clearCache() { this.loggerCache.clear(); } } exports.TemporalLoggerManager = TemporalLoggerManager; TemporalLoggerManager.DEFAULT_CONFIG = { enableLogger: true, logLevel: 'info', appName: 'NestJS-Temporal', }; class TemporalLogger { constructor(context, config = {}) { this.context = context; this.nestLogger = new common_1.Logger(context); const logLevel = config.logLevel && TemporalLogger.LEVEL_INDICES.has(config.logLevel) ? config.logLevel : 'info'; this.config = { enableLogger: config.enableLogger ?? true, logLevel, }; this.currentLevelIndex = TemporalLogger.LEVEL_INDICES.get(this.config.logLevel) ?? 2; } shouldLog(level) { if (!this.config.enableLogger) return false; const requestedLevelIndex = TemporalLogger.LEVEL_INDICES.get(level); return requestedLevelIndex !== undefined && requestedLevelIndex <= this.currentLevelIndex; } error(message, trace, context) { if (this.shouldLog('error')) { const errorContext = context ?? this.context; const stackTrace = trace instanceof Error ? trace.stack : trace; this.nestLogger.error(message, stackTrace, errorContext); } } warn(message, context) { if (this.shouldLog('warn')) { this.nestLogger.warn(message, context ?? this.context); } } log(message, context) { if (this.shouldLog('info')) { this.nestLogger.log(message, context ?? this.context); } } info(message, context) { this.log(message, context); } debug(message, context) { if (this.shouldLog('debug')) { this.nestLogger.debug(message, context ?? this.context); } } verbose(message, context) { if (this.shouldLog('verbose')) { this.nestLogger.verbose(message, context ?? this.context); } } logExecutionTime(methodName, startTime, context) { if (this.shouldLog('debug')) { const executionTime = Date.now() - startTime; this.nestLogger.debug(`${methodName} executed in ${executionTime}ms`, context ?? this.context); } } logWithLevel(level, message, context) { if (!this.shouldLog(level)) return; const methods = { error: () => this.error(message, undefined, context), warn: () => this.warn(message, context), info: () => this.log(message, context), debug: () => this.debug(message, context), verbose: () => this.verbose(message, context), }; methods[level](); } createChildLogger(childContext, config) { const combinedContext = `${this.context}:${childContext}`; const childConfig = config ? { ...this.config, ...config } : this.config; return new TemporalLogger(combinedContext, childConfig); } getConfig() { return this.config; } isEnabled() { return this.config.enableLogger; } getLogLevel() { return this.config.logLevel; } getContext() { return this.context; } isLevelEnabled(level) { return this.shouldLog(level); } } exports.TemporalLogger = TemporalLogger; TemporalLogger.LOG_LEVELS = [ 'error', 'warn', 'info', 'debug', 'verbose', ]; TemporalLogger.LEVEL_INDICES = new Map(TemporalLogger.LOG_LEVELS.map((level, index) => [level, index])); function createLogger(context, config) { return TemporalLoggerManager.getInstance().createLogger(context, config); } class LoggerUtils { static logServiceInit(logger, serviceName, config) { logger.log(`Initializing ${serviceName}...`); if (config && logger.getLogLevel() === 'debug') { logger.debug(`${serviceName} configuration:`, JSON.stringify(config, null, 2)); } } static logServiceShutdown(logger, serviceName) { logger.log(`Shutting down ${serviceName}...`); } static logConnection(logger, target, success, error) { if (success) { logger.log(`Successfully connected to ${target}`); } else { logger.error(`Failed to connect to ${target}`, error); } } static logOperation(logger, operation, success, details, error) { if (success) { logger.log(`${operation} completed successfully${details ? `: ${details}` : ''}`); } else { logger.error(`${operation} failed${details ? `: ${details}` : ''}`, error); } } } exports.LoggerUtils = LoggerUtils; //# sourceMappingURL=logger.js.map