From 1958d566475c2f3a5039dd49d06a01945e4f2148 Mon Sep 17 00:00:00 2001 From: Rafa Mel Date: Fri, 17 May 2019 05:19:02 +0200 Subject: [PATCH] fix(utils/logger): prevents logger methodFactory from being registered twice --- src/utils/logger.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 0136670..2f8720c 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -31,13 +31,23 @@ function prefix(level: TLogger): string { return (color ? color(`[${name}]`) : `[${name}]`) + ` ${APP_NAME}: `; } -const factory = logger.methodFactory; -logger.methodFactory = (...args) => (...inner: any[]) => { - factory.call(loglevel, ...args)( - prefix(args[0].toLowerCase() as TLogger) + inner[0], - ...inner.slice(1) - ); -}; +type Factory = loglevel.MethodFactory & { registered?: boolean }; +const factory: Factory = logger.methodFactory; + +// Prevent method factory to register twice for the same logger +// as it could occur with different instances +if (!factory.registered) { + const methodFactory: Factory = function(...args) { + return (...inner: any[]) => { + factory.call(loglevel, ...args)( + prefix(args[0].toLowerCase() as TLogger) + inner[0], + ...inner.slice(1) + ); + }; + }; + methodFactory.registered = true; + logger.methodFactory = methodFactory; +} // Must be set -at least once- after overwriting methodFactory logger.setDefaultLevel(DEFAULT_LOG_LEVEL);