Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use default levels if useOnlyCustomLevels is specified #522

Merged
merged 1 commit into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you verifyies this property is defined and of the correct value here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had put a console.log in the code and for my test its true and for the other tests where it is actually defined. I'm just confused that the test actually returns null for priority. That's really what I don't understand.


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