Skip to content

Commit

Permalink
Fix set level in browser logger (#424)
Browse files Browse the repository at this point in the history
* Add level and levelVal propreties to browser logger

* Fix set level
  • Loading branch information
slavasitnikov authored and mcollina committed May 30, 2018
1 parent 309ca25 commit 1ab4beb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
12 changes: 6 additions & 6 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ pino.levels = {
pino.stdSerializers = stdSerializers

function set (opts, logger, level, fallback) {
var val = logger.levelVal
logger[level] = val > pino.levels.values[level] ? noop
: (logger[level] ? logger[level] : (_console[level] || _console[fallback] || noop))
var proto = Object.getPrototypeOf(logger)
logger[level] = logger.levelVal > logger.levels.values[level] ? noop
: (proto[level] ? proto[level] : (_console[level] || _console[fallback] || noop))

wrap(opts, logger, logger.val, level)
wrap(opts, logger, level)
}

function wrap (opts, logger, val, level) {
function wrap (opts, logger, level) {
if (!opts.transmit && logger[level] === noop) return

logger[level] = (function (write) {
Expand Down Expand Up @@ -190,7 +190,7 @@ function wrap (opts, logger, val, level) {
transmitLevel: transmitLevel,
transmitValue: pino.levels.values[opts.transmit.level || logger.level],
send: opts.transmit.send,
val: val
val: logger.levelVal
}, args)
}
}
Expand Down
60 changes: 60 additions & 0 deletions test/browser.levels.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,66 @@ test('set the level by string', function (t) {
t.end()
})

test('set the level by string. init with silent', function (t) {
t.plan(4)
var expected = [
{
level: 50,
msg: 'this is an error'
},
{
level: 60,
msg: 'this is fatal'
}
]
var instance = pino({
level: 'silent',
browser: {
write: function (actual) {
checkLogObjects(t, actual, expected.shift())
}
}
})

instance.level = 'error'
instance.info('hello world')
instance.error('this is an error')
instance.fatal('this is fatal')
t.end()
})

test('set the level by string. init with silent and transmit', function (t) {
t.plan(4)
var expected = [
{
level: 50,
msg: 'this is an error'
},
{
level: 60,
msg: 'this is fatal'
}
]
var instance = pino({
level: 'silent',
browser: {
write: function (actual) {
checkLogObjects(t, actual, expected.shift())
}
},
transmit: {
send: function () {
}
}
})

instance.level = 'error'
instance.info('hello world')
instance.error('this is an error')
instance.fatal('this is fatal')
t.end()
})

test('set the level via constructor', function (t) {
t.plan(4)
var expected = [
Expand Down

0 comments on commit 1ab4beb

Please sign in to comment.