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

FEAT: Support ignorePaths option to exclude certain paths from logging. #30

Merged
merged 4 commits into from
Nov 15, 2017
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
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const pino = require('pino')
const nullLogger = require('abstract-logging')

const levels = ['trace', 'debug', 'info', 'warn', 'error']
const levelTags = {
Expand Down Expand Up @@ -49,13 +50,24 @@ async function register (server, options) {
throw new Error('invalid tag levels')
}

var ignoreTable = {}
if (options.ignorePaths) {
for (let i = 0; i < options.ignorePaths.length; i++) {
ignoreTable[options.ignorePaths[i]] = true
}
}

const mergeHapiLogData = options.mergeHapiLogData

// expose logger as 'server.logger()'
server.decorate('server', 'logger', () => logger)

// set a logger for each request
server.ext('onRequest', (request, h) => {
if (options.ignorePaths && ignoreTable[request.url.path]) {
request.logger = nullLogger
return h.continue
}
request.logger = logger.child({ req: request })
return h.continue
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"standard": "^10.0.3"
},
"dependencies": {
"abstract-logging": "^1.0.0",
"pino": "^4.10.0"
},
"repository": {
Expand Down
38 changes: 37 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ experiment('uses a prior pino instance', () => {
}

await server.register(plugin)
server.logger().info({foo: 'bar'}, 'hello world')
server.logger().info({ foo: 'bar' }, 'hello world')
await finish
})
})
Expand Down Expand Up @@ -789,3 +789,39 @@ experiment('logging with request payload', () => {
await done
})
})

experiment('ignore request logs for paths in ignorePaths', () => {
test('when path matches entry in ignorePaths, nothing should be logged', async () => {
const server = getServer()
let resolver
const done = new Promise((resolve, reject) => {
resolver = resolve
})
const stream = sink((data) => {
expect(data.req.url).to.not.equal('/ignored')
resolver()
})
const logger = require('pino')(stream)
const plugin = {
plugin: Pino,
options: {
instance: logger,
ignorePaths: ['/ignored']
}
}

await server.register(plugin)

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

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

})
await done
})
})