Skip to content

Commit

Permalink
Fix UnhandledPromiseRejection warning when logging on an ignored path (
Browse files Browse the repository at this point in the history
  • Loading branch information
qswitcher authored and mcollina committed Aug 3, 2019
1 parent 79ad1b4 commit 20f1f27
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ async function register (server, options) {
}

function logEvent (current, event) {
// check for null logger
if (current === nullLogger) {
return
}

var tags = event.tags
var data = event.data

Expand Down
58 changes: 58 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,64 @@ experiment('ignore response logs for paths in ignorePaths', () => {
})
})

experiment('ignore request.log logs for paths in ignorePaths', () => {
test('when path matches entry in ignorePaths, nothing should be logged', async () => {
const level = 'info'
const server = getServer()
server.route({
path: '/ignored',
method: 'GET',
handler: (req, h) => {
req.log([level], 'hello logger')
return 'hello world'
}
})

server.route({
path: '/foo',
method: 'GET',
handler: (req, h) => {
req.log([level], 'foo')
return 'foo'
}
})

let resolver
const done = new Promise((resolve, reject) => {
resolver = resolve
})
const stream = sink((data) => {
expect(data.req.url).to.endWith('/foo')
expect(data.tags).to.equal([level])
expect(data.data).to.equal('foo')
resolver()
})
const logger = require('pino')(stream)
const plugin = {
plugin: Pino,
options: {
instance: logger,
logEvents: ['request-error'],
ignorePaths: ['/ignored']
}
}

await server.register(plugin)

await server.inject({
method: 'GET',
url: '/ignored'
})

await server.inject({
method: 'GET',
url: '/foo'

})
await done
})
})

experiment('logging with logRouteTags option enabled', () => {
test('when logRouteTags is true, tags are part of the logged object', async () => {
const server = getServer()
Expand Down

0 comments on commit 20f1f27

Please sign in to comment.