From e8546f112cd8f089825f5a242b08c2aedb393553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Bouchez?= Date: Thu, 5 Jan 2023 10:32:23 +0100 Subject: [PATCH] fix(force): prevent syncLogger reset canForceWrite --- src/definitions.ts | 1 + src/index.ts | 4 ++-- test/logger.js | 13 +++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/definitions.ts b/src/definitions.ts index 0af5f91..9f4b524 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -23,6 +23,7 @@ export interface Logger { warn: LogMethod error: LogMethod isLevelEnabled(level: string): boolean | undefined + canForceWrite?: boolean } export interface OutputAdapter { diff --git a/src/index.ts b/src/index.ts index 49aaf7a..79caa50 100644 --- a/src/index.ts +++ b/src/index.ts @@ -280,7 +280,7 @@ export const syncLogger = (logger: Logger, namespace: string, canForceWrite?: bo logger.isLevelEnabled = (level) => enabledLevels[level] } - + logger.canForceWrite = canForceWrite return logger } @@ -290,7 +290,7 @@ export const syncLogger = (logger: Logger, namespace: string, canForceWrite?: bo */ export const syncLoggers = () => { for (const [namespace, logger] of Object.entries(internals.loggers)) { - syncLogger(logger, namespace) + syncLogger(logger, namespace, logger.canForceWrite) } } diff --git a/test/logger.js b/test/logger.js index 323abe2..ec760f5 100644 --- a/test/logger.js +++ b/test/logger.js @@ -84,6 +84,19 @@ test('A logger instance should log if instance can forceWrite and forceLogging i logger.write.restore() }) +test('setLevel and setNamespace should not reset canForceWrite', (t) => { + const log = logger.createLogger('test', true) + logger.setNamespaces('*') + logger.setLevel('info') + const spy = sinon.spy(logger, 'write') + + log.debug(null, {}, true) + + t.is(spy.callCount, 1) + + logger.write.restore() +}) + test("A logger instance shouldn't log if namespace is not enabled", (t) => { logger.setNamespaces('test:*')