Skip to content

Commit

Permalink
Merge pull request #44 from pebie/patch-force-write
Browse files Browse the repository at this point in the history
fix(force): prevent syncLogger reset canForceWrite
  • Loading branch information
pebie authored Jan 5, 2023
2 parents d6a7dad + e8546f1 commit aa89e8c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface Logger {
warn: LogMethod
error: LogMethod
isLevelEnabled(level: string): boolean | undefined
canForceWrite?: boolean
}

export interface OutputAdapter {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export const syncLogger = (logger: Logger, namespace: string, canForceWrite?: bo

logger.isLevelEnabled = (level) => enabledLevels[level]
}

logger.canForceWrite = canForceWrite
return logger
}

Expand All @@ -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)
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:*')

Expand Down

0 comments on commit aa89e8c

Please sign in to comment.