Skip to content

Commit

Permalink
setLevel should respect level comparison option (#1901)
Browse files Browse the repository at this point in the history
* fix: `setLevel` should respect level comparison option

* test: add child logger test on respect level comparison

* Update lib/levels.js

---------

Co-authored-by: James Sumners <321201+jsumners@users.noreply.github.com>
  • Loading branch information
obrus-corcentric and jsumners committed Feb 10, 2024
1 parent 7df8840 commit 79fe14e
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) === false) {
this[key] = noop
continue
}
Expand Down
171 changes: 171 additions & 0 deletions test/levels.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,177 @@ 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 in parent logger', 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 in parent logger', 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 in parent 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)

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('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()
})

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 }
Expand Down

0 comments on commit 79fe14e

Please sign in to comment.