Skip to content

Commit 79fe14e

Browse files
setLevel should respect level comparison option (#1901)
* 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>
1 parent 7df8840 commit 79fe14e

File tree

2 files changed

+173
-1
lines changed

2 files changed

+173
-1
lines changed

lib/levels.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ function setLevel (level) {
8484
const preLevelVal = this[levelValSym]
8585
const levelVal = this[levelValSym] = values[level]
8686
const useOnlyCustomLevelsVal = this[useOnlyCustomLevelsSym]
87+
const levelComparison = this[levelCompSym]
8788
const hook = this[hooksSym].logMethod
8889

8990
for (const key in values) {
90-
if (levelVal > values[key]) {
91+
if (levelComparison(values[key], levelVal) === false) {
9192
this[key] = noop
9293
continue
9394
}

test/levels.test.js

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,177 @@ test('changing level from info to silent and back to info in child logger', asyn
527527
check(equal, result, expected.level, expected.msg)
528528
})
529529

530+
test('changing level respects level comparison set to', async ({ test, end }) => {
531+
const ascLevels = {
532+
debug: 1,
533+
info: 2,
534+
warn: 3
535+
}
536+
537+
const descLevels = {
538+
debug: 3,
539+
info: 2,
540+
warn: 1
541+
}
542+
543+
const expected = {
544+
level: 2,
545+
msg: 'hello world'
546+
}
547+
548+
test('ASC in parent logger', async ({ equal }) => {
549+
const customLevels = ascLevels
550+
const levelComparison = 'ASC'
551+
552+
const stream = sink()
553+
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream)
554+
555+
logger.level = 'warn'
556+
logger.info('hello world')
557+
let result = stream.read()
558+
equal(result, null)
559+
560+
logger.level = 'debug'
561+
logger.info('hello world')
562+
result = await once(stream, 'data')
563+
check(equal, result, expected.level, expected.msg)
564+
})
565+
566+
test('DESC in parent logger', async ({ equal }) => {
567+
const customLevels = descLevels
568+
const levelComparison = 'DESC'
569+
570+
const stream = sink()
571+
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream)
572+
573+
logger.level = 'warn'
574+
logger.info('hello world')
575+
let result = stream.read()
576+
equal(result, null)
577+
578+
logger.level = 'debug'
579+
logger.info('hello world')
580+
result = await once(stream, 'data')
581+
check(equal, result, expected.level, expected.msg)
582+
})
583+
584+
test('custom function in parent logger', async ({ equal }) => {
585+
const customLevels = {
586+
info: 2,
587+
debug: 345,
588+
warn: 789
589+
}
590+
const levelComparison = (current, expected) => {
591+
if (expected === customLevels.warn) return false
592+
return true
593+
}
594+
595+
const stream = sink()
596+
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream)
597+
598+
logger.level = 'warn'
599+
logger.info('hello world')
600+
let result = stream.read()
601+
equal(result, null)
602+
603+
logger.level = 'debug'
604+
logger.info('hello world')
605+
result = await once(stream, 'data')
606+
check(equal, result, expected.level, expected.msg)
607+
})
608+
609+
test('ASC in child logger', async ({ equal }) => {
610+
const customLevels = ascLevels
611+
const levelComparison = 'ASC'
612+
613+
const stream = sink()
614+
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream).child({ })
615+
616+
logger.level = 'warn'
617+
logger.info('hello world')
618+
let result = stream.read()
619+
equal(result, null)
620+
621+
logger.level = 'debug'
622+
logger.info('hello world')
623+
result = await once(stream, 'data')
624+
check(equal, result, expected.level, expected.msg)
625+
})
626+
627+
test('DESC in parent logger', async ({ equal }) => {
628+
const customLevels = descLevels
629+
const levelComparison = 'DESC'
630+
631+
const stream = sink()
632+
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream).child({ })
633+
634+
logger.level = 'warn'
635+
logger.info('hello world')
636+
let result = stream.read()
637+
equal(result, null)
638+
639+
logger.level = 'debug'
640+
logger.info('hello world')
641+
result = await once(stream, 'data')
642+
check(equal, result, expected.level, expected.msg)
643+
})
644+
645+
test('custom function in child logger', async ({ equal }) => {
646+
const customLevels = {
647+
info: 2,
648+
debug: 345,
649+
warn: 789
650+
}
651+
const levelComparison = (current, expected) => {
652+
if (expected === customLevels.warn) return false
653+
return true
654+
}
655+
656+
const stream = sink()
657+
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream).child({ })
658+
659+
logger.level = 'warn'
660+
logger.info('hello world')
661+
let result = stream.read()
662+
equal(result, null)
663+
664+
logger.level = 'debug'
665+
logger.info('hello world')
666+
result = await once(stream, 'data')
667+
check(equal, result, expected.level, expected.msg)
668+
})
669+
670+
end()
671+
})
672+
673+
test('changing level respects level comparison DESC', async ({ equal }) => {
674+
const customLevels = {
675+
warn: 1,
676+
info: 2,
677+
debug: 3
678+
}
679+
680+
const levelComparison = 'DESC'
681+
682+
const expected = {
683+
level: 2,
684+
msg: 'hello world'
685+
}
686+
687+
const stream = sink()
688+
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream)
689+
690+
logger.level = 'warn'
691+
logger.info('hello world')
692+
let result = stream.read()
693+
equal(result, null)
694+
695+
logger.level = 'debug'
696+
logger.info('hello world')
697+
result = await once(stream, 'data')
698+
check(equal, result, expected.level, expected.msg)
699+
})
700+
530701
// testing for potential loss of Pino constructor scope from serializers - an edge case with circular refs see: https://github.com/pinojs/pino/issues/833
531702
test('trying to get levels when `this` is no longer a Pino instance returns an empty string', async ({ equal }) => {
532703
const notPinoInstance = { some: 'object', getLevel: levelsLib.getLevel }

0 commit comments

Comments
 (0)