Skip to content

Commit

Permalink
Don't use default levels if useOnlyCustomLevels is specified (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
cberg-zalando authored and mcollina committed Oct 5, 2018
1 parent 916451a commit 2f5f054
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/levels.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
const flatstr = require('flatstr')
const { lsCacheSym, levelValSym, useLevelLabelsSym, changeLevelNameSym } = require('./symbols')
const { lsCacheSym, levelValSym, useLevelLabelsSym, changeLevelNameSym, useOnlyCustomLevelsSym } = require('./symbols')
const { noop, genLog } = require('./tools')

const levels = {
Expand Down Expand Up @@ -42,7 +42,11 @@ function genLsCache (instance) {
return instance
}

function isStandardLevel (level) {
function isStandardLevel (level, useOnlyCustomLevels) {
if (useOnlyCustomLevels) {
return false
}

switch (level) {
case 'fatal':
case 'error':
Expand All @@ -65,13 +69,14 @@ function setLevel (level) {
if (values[level] === undefined) throw Error('unknown level ' + level)
const preLevelVal = this[levelValSym]
const levelVal = this[levelValSym] = values[level]
const useOnlyCustomLevelsVal = this[useOnlyCustomLevelsSym]

for (var key in values) {
if (levelVal > values[key]) {
this[key] = noop
continue
}
this[key] = isStandardLevel(key) ? levelMethods[key] : genLog(values[key])
this[key] = isStandardLevel(key, useOnlyCustomLevelsVal) ? levelMethods[key] : genLog(values[key])
}

this.emit(
Expand Down
17 changes: 17 additions & 0 deletions test/custom-levels.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ test('custom levels does not override default levels', async ({ is }) => {
is(level, 30)
})

test('default levels can be redefined using custom levels', async ({ is }) => {
const stream = sink()
const logger = pino({
customLevels: {
info: 35,
debug: 45
},
useOnlyCustomLevels: true
}, stream)

is(logger.hasOwnProperty('info'), true)

logger.info('test')
const { level } = await once(stream, 'data')
is(level, 35)
})

test('custom levels overrides default level label if use useOnlyCustomLevels', async ({ is }) => {
const stream = sink()
const logger = pino({
Expand Down

0 comments on commit 2f5f054

Please sign in to comment.