From f5b9bc7b69a6fdadd61d3d0c33b67fe0e092d153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Da=C3=9Fler?= Date: Sun, 12 Oct 2025 23:51:24 +0200 Subject: [PATCH] Fix getLogger() singleton bug causing all loggers to share the same context MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The getLogger() function was using a single defaultLogger variable that would only be initialized once with the first context. All subsequent calls with different contexts would return the same logger instance, causing all logs to show the same context prefix. Changed to use a Map-based cache that stores separate logger instances per context, ensuring each context gets its own logger with the correct context name. Bump version to 1.0.1-beta.3 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- logger.ts | 15 ++++++++++----- package.json | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/logger.ts b/logger.ts index 308e99f..0504a2b 100644 --- a/logger.ts +++ b/logger.ts @@ -177,14 +177,19 @@ export class LoggerComponent implements ILoggerComponent { } } -// Export a singleton instance for convenience -let defaultLogger: LoggerComponent | null = null; +// Cache logger instances by context to avoid recreating them +const loggerCache = new Map(); export function getLogger(context = 'main'): ILoggerComponent { - if (!defaultLogger) { - defaultLogger = new LoggerComponent(context); + // Return cached logger for this context if it exists + if (loggerCache.has(context)) { + return loggerCache.get(context)!; } - return defaultLogger; + + // Create new logger for this context + const newLogger = new LoggerComponent(context); + loggerCache.set(context, newLogger); + return newLogger; } // Export default instance diff --git a/package.json b/package.json index 68856e6..746c11c 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "@metatrom/logger", - "version": "1.0.1-beta.2", + "version": "1.0.1-beta.3", "main": "index.ts", "type": "module", "metatrom": { - "ior": "com.metatrom.universal-components.logger@1.0.1-beta.2", + "ior": "com.metatrom.universal-components.logger@1.0.1-beta.3", "capabilities": { "p2p": false, "contracts": false,