From 70274749426cb0cfd254f8daa4fc44fa0cdf8faf Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Sun, 6 Apr 2025 13:44:30 -0700 Subject: [PATCH] Logger: per-module factory --- src/common/logger/index.ts | 3 +++ src/common/logger/logger.factory.ts | 40 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/common/logger/logger.factory.ts diff --git a/src/common/logger/index.ts b/src/common/logger/index.ts index 8eeed4a37..8b63c03f3 100644 --- a/src/common/logger/index.ts +++ b/src/common/logger/index.ts @@ -7,3 +7,6 @@ export { setupClientUncaughtErrorsLogging } from './interceptors/logger.unhandle // re-export the core functionality export { logger } from './logger.client'; + +// re-export the module logger factory +export { createModuleLogger } from './logger.factory'; diff --git a/src/common/logger/logger.factory.ts b/src/common/logger/logger.factory.ts new file mode 100644 index 000000000..704b76621 --- /dev/null +++ b/src/common/logger/logger.factory.ts @@ -0,0 +1,40 @@ +import type { ClientLogger, LogOptions, LogSource } from './logger.types'; +import { logger } from './logger.client'; + + +/** + * Creates a module-specific logger with a predefined source and optional event prefix. + * + * @param source The source identifier for all logs from this module + * @param eventPrefix Optional prefix to prepend to all log messages + * @returns A logger instance with preset source and prefix + */ +export function createModuleLogger(source: LogSource | string, eventPrefix: string = ''): ClientLogger { + // format message with prefix if provided + const prefixMessage = eventPrefix + ? (message: string): string => `${eventPrefix}: ${message}` + : (message: string): string => message; + + return { + debug: (message: string, details?: any, _overrideSource?: LogSource, options?: LogOptions) => + logger.debug(prefixMessage(message), details, source as LogSource, options), + + info: (message: string, details?: any, _overrideSource?: LogSource, options?: LogOptions) => + logger.info(prefixMessage(message), details, source as LogSource, options), + + warn: (message: string, details?: any, _overrideSource?: LogSource, options?: LogOptions) => + logger.warn(prefixMessage(message), details, source as LogSource, options), + + error: (message: string, details?: any, _overrideSource?: LogSource, options?: LogOptions) => + logger.error(prefixMessage(message), details, source as LogSource, options), + + critical: (message: string, details?: any, _overrideSource?: LogSource, options?: LogOptions) => + logger.critical(prefixMessage(message), details, source as LogSource, options), + + // forward action methods directly to the main logger + executeAction: logger.executeAction, + markActionCompleted: logger.markActionCompleted, + markDismissed: logger.markDismissed, + getPendingActions: logger.getPendingActions, + }; +}