1 Commits

Author SHA1 Message Date
Chris Daßler
f5b9bc7b69 Fix getLogger() singleton bug causing all loggers to share the same context
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 <noreply@anthropic.com>
2025-10-12 23:51:24 +02:00
2 changed files with 12 additions and 7 deletions

View File

@@ -177,14 +177,19 @@ export class LoggerComponent implements ILoggerComponent {
} }
} }
// Export a singleton instance for convenience // Cache logger instances by context to avoid recreating them
let defaultLogger: LoggerComponent | null = null; const loggerCache = new Map<string, LoggerComponent>();
export function getLogger(context = 'main'): ILoggerComponent { export function getLogger(context = 'main'): ILoggerComponent {
if (!defaultLogger) { // Return cached logger for this context if it exists
defaultLogger = new LoggerComponent(context); 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 // Export default instance

View File

@@ -1,10 +1,10 @@
{ {
"name": "@metatrom/logger", "name": "@metatrom/logger",
"version": "1.0.1-beta.2", "version": "1.0.1-beta.3",
"main": "index.ts", "main": "index.ts",
"type": "module", "type": "module",
"metatrom": { "metatrom": {
"ior": "com.metatrom.universal-components.logger@1.0.1-beta.2", "ior": "com.metatrom.universal-components.logger@1.0.1-beta.3",
"capabilities": { "capabilities": {
"p2p": false, "p2p": false,
"contracts": false, "contracts": false,