From bdd2b6e4e7cb4c8fddf7d7657a5c5a004657e80c Mon Sep 17 00:00:00 2001 From: obrus Date: Fri, 9 Feb 2024 14:06:37 +0200 Subject: [PATCH 1/3] fix: `setLevel` should respect level comparison option --- lib/levels.js | 3 +- test/levels.test.js | 110 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/lib/levels.js b/lib/levels.js index 5555da49c..ad7d94caa 100644 --- a/lib/levels.js +++ b/lib/levels.js @@ -84,10 +84,11 @@ function setLevel (level) { const preLevelVal = this[levelValSym] const levelVal = this[levelValSym] = values[level] const useOnlyCustomLevelsVal = this[useOnlyCustomLevelsSym] + const levelComparison = this[levelCompSym] const hook = this[hooksSym].logMethod for (const key in values) { - if (levelVal > values[key]) { + if (!levelComparison(values[key], levelVal)) { this[key] = noop continue } diff --git a/test/levels.test.js b/test/levels.test.js index e47945dfd..f0e590d00 100644 --- a/test/levels.test.js +++ b/test/levels.test.js @@ -527,6 +527,116 @@ test('changing level from info to silent and back to info in child logger', asyn check(equal, result, expected.level, expected.msg) }) +test('changing level respects level comparison set to', async ({ test, end }) => { + const ascLevels = { + debug: 1, + info: 2, + warn: 3 + } + + const descLevels = { + debug: 3, + info: 2, + warn: 1 + } + + const expected = { + level: 2, + msg: 'hello world' + } + + test('ASC', async ({ equal }) => { + const customLevels = ascLevels + const levelComparison = 'ASC' + + const stream = sink() + const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream) + + logger.level = 'warn' + logger.info('hello world') + let result = stream.read() + equal(result, null) + + logger.level = 'debug' + logger.info('hello world') + result = await once(stream, 'data') + check(equal, result, expected.level, expected.msg) + }) + + test('DESC', async ({ equal }) => { + const customLevels = descLevels + const levelComparison = 'DESC' + + const stream = sink() + const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream) + + logger.level = 'warn' + logger.info('hello world') + let result = stream.read() + equal(result, null) + + logger.level = 'debug' + logger.info('hello world') + result = await once(stream, 'data') + check(equal, result, expected.level, expected.msg) + }) + + test('custom function', async ({ equal }) => { + const customLevels = { + info: 2, + debug: 345, + warn: 789 + } + const levelComparison = (current, expected) => { + if (expected === customLevels.warn) return false + return true + } + + const stream = sink() + const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream) + + logger.level = 'warn' + logger.info('hello world') + let result = stream.read() + equal(result, null) + + logger.level = 'debug' + logger.info('hello world') + result = await once(stream, 'data') + check(equal, result, expected.level, expected.msg) + }) + + end() +}) + +test('changing level respects level comparison DESC', async ({ equal }) => { + const customLevels = { + warn: 1, + info: 2, + debug: 3 + } + + const levelComparison = 'DESC' + + const expected = { + level: 2, + msg: 'hello world' + } + + const stream = sink() + const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream) + + logger.level = 'warn' + logger.info('hello world') + let result = stream.read() + equal(result, null) + + logger.level = 'debug' + logger.info('hello world') + result = await once(stream, 'data') + check(equal, result, expected.level, expected.msg) +}) + // testing for potential loss of Pino constructor scope from serializers - an edge case with circular refs see: https://github.com/pinojs/pino/issues/833 test('trying to get levels when `this` is no longer a Pino instance returns an empty string', async ({ equal }) => { const notPinoInstance = { some: 'object', getLevel: levelsLib.getLevel } From 8c9238e4919c6ec0fb3a3861533cab06986675fa Mon Sep 17 00:00:00 2001 From: obrus Date: Fri, 9 Feb 2024 14:11:14 +0200 Subject: [PATCH 2/3] test: add child logger test on respect level comparison --- test/levels.test.js | 67 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/test/levels.test.js b/test/levels.test.js index f0e590d00..1581bc134 100644 --- a/test/levels.test.js +++ b/test/levels.test.js @@ -545,7 +545,7 @@ test('changing level respects level comparison set to', async ({ test, end }) => msg: 'hello world' } - test('ASC', async ({ equal }) => { + test('ASC in parent logger', async ({ equal }) => { const customLevels = ascLevels const levelComparison = 'ASC' @@ -563,7 +563,7 @@ test('changing level respects level comparison set to', async ({ test, end }) => check(equal, result, expected.level, expected.msg) }) - test('DESC', async ({ equal }) => { + test('DESC in parent logger', async ({ equal }) => { const customLevels = descLevels const levelComparison = 'DESC' @@ -581,7 +581,7 @@ test('changing level respects level comparison set to', async ({ test, end }) => check(equal, result, expected.level, expected.msg) }) - test('custom function', async ({ equal }) => { + test('custom function in parent logger', async ({ equal }) => { const customLevels = { info: 2, debug: 345, @@ -606,6 +606,67 @@ test('changing level respects level comparison set to', async ({ test, end }) => check(equal, result, expected.level, expected.msg) }) + test('ASC in child logger', async ({ equal }) => { + const customLevels = ascLevels + const levelComparison = 'ASC' + + const stream = sink() + const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream).child({ }) + + logger.level = 'warn' + logger.info('hello world') + let result = stream.read() + equal(result, null) + + logger.level = 'debug' + logger.info('hello world') + result = await once(stream, 'data') + check(equal, result, expected.level, expected.msg) + }) + + test('DESC in parent logger', async ({ equal }) => { + const customLevels = descLevels + const levelComparison = 'DESC' + + const stream = sink() + const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream).child({ }) + + logger.level = 'warn' + logger.info('hello world') + let result = stream.read() + equal(result, null) + + logger.level = 'debug' + logger.info('hello world') + result = await once(stream, 'data') + check(equal, result, expected.level, expected.msg) + }) + + test('custom function in child logger', async ({ equal }) => { + const customLevels = { + info: 2, + debug: 345, + warn: 789 + } + const levelComparison = (current, expected) => { + if (expected === customLevels.warn) return false + return true + } + + const stream = sink() + const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream).child({ }) + + logger.level = 'warn' + logger.info('hello world') + let result = stream.read() + equal(result, null) + + logger.level = 'debug' + logger.info('hello world') + result = await once(stream, 'data') + check(equal, result, expected.level, expected.msg) + }) + end() }) From d35c3c424e84665b0e57ebc49b3e5c1c93eb19ce Mon Sep 17 00:00:00 2001 From: James Sumners <321201+jsumners@users.noreply.github.com> Date: Sat, 10 Feb 2024 08:05:32 -0500 Subject: [PATCH 3/3] Update lib/levels.js --- lib/levels.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/levels.js b/lib/levels.js index ad7d94caa..4a3af01ea 100644 --- a/lib/levels.js +++ b/lib/levels.js @@ -88,7 +88,7 @@ function setLevel (level) { const hook = this[hooksSym].logMethod for (const key in values) { - if (!levelComparison(values[key], levelVal)) { + if (levelComparison(values[key], levelVal) === false) { this[key] = noop continue }