Skip to content

Commit

Permalink
fix(#1347): added disabled to browser opts for logs (#1480)
Browse files Browse the repository at this point in the history
  • Loading branch information
dancastillo committed Jun 30, 2022
1 parent 456ada5 commit 31f7cd6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion browser.js
Expand Up @@ -53,7 +53,7 @@ function pino (opts) {
proto.error = proto.fatal = proto.warn =
proto.info = proto.debug = proto.trace = proto
}
if (opts.enabled === false) opts.level = 'silent'
if (opts.enabled === false || opts.browser.disabled) opts.level = 'silent'
const level = opts.level || 'info'
const logger = Object.create(proto)
if (!logger.log) logger.log = noop
Expand Down
9 changes: 9 additions & 0 deletions docs/browser.md
Expand Up @@ -197,3 +197,12 @@ const pino = require('pino')({
}
})
```

### `disabled` (Boolean)

```js
const pino = require('pino')({browser: {disabled: true}})
```

The `disabled` option will disable logging in browser if set
to `true`. Default is set to `false`.
87 changes: 87 additions & 0 deletions test/browser-disabled.test.js
@@ -0,0 +1,87 @@
'use strict'
const test = require('tape')
const pino = require('../browser')

test('set browser opts disabled to true', ({ end, same }) => {
const instance = pino({
browser: {
disabled: true,
write (actual) {
checkLogObjects(same, actual, [])
}
}
})
instance.info('hello world')
instance.error('this is an error')
instance.fatal('this is fatal')

end()
})

test('set browser opts disabled to false', ({ end, same }) => {
const expected = [
{
level: 30,
msg: 'hello world'
},
{
level: 50,
msg: 'this is an error'
},
{
level: 60,
msg: 'this is fatal'
}
]
const instance = pino({
browser: {
disabled: false,
write (actual) {
checkLogObjects(same, actual, expected.shift())
}
}
})
instance.info('hello world')
instance.error('this is an error')
instance.fatal('this is fatal')

end()
})

test('disabled is not set in browser opts', ({ end, same }) => {
const expected = [
{
level: 30,
msg: 'hello world'
},
{
level: 50,
msg: 'this is an error'
},
{
level: 60,
msg: 'this is fatal'
}
]
const instance = pino({
browser: {
write (actual) {
checkLogObjects(same, actual, expected.shift())
}
}
})
instance.info('hello world')
instance.error('this is an error')
instance.fatal('this is fatal')

end()
})

function checkLogObjects (same, actual, expected, is) {
const actualCopy = Object.assign({}, actual)
const expectedCopy = Object.assign({}, expected)
delete actualCopy.time
delete expectedCopy.time

same(actualCopy, expectedCopy)
}
1 change: 1 addition & 0 deletions test/types/pino.test-d.ts
Expand Up @@ -103,6 +103,7 @@ pino({
logEvent.messages;
},
},
disabled: false
},
});

Expand Down

0 comments on commit 31f7cd6

Please sign in to comment.