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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ async function register (server, options) {
if (!validTags || (allTags && levels.indexOf(allTags) < 0)) {
throw new Error('invalid tag levels')
}
var nullLogger
var ignoreTable = {}
if (options.ignorePaths) {
nullLogger = buildNullLogger(levels)
for (let i = 0; i < options.ignorePaths.length; i++) {
ignoreTable[options.ignorePaths[i]] = true
}
}

const mergeHapiLogData = options.mergeHapiLogData

Expand All @@ -53,6 +61,10 @@ async function register (server, options) {

// 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 Expand Up @@ -152,6 +164,18 @@ function asReqValue (req) {
}
}

function buildNullLogger (levels) {
var logger = {}

var noop = function () { }

for (let i = 0; i < levels.length; i++) {
logger[levels[i]] = noop
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

instead of building a null logger every time, you can use https://www.npmjs.com/package/abstract-logging.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see now. I was trying to keep hapi-pino in that golden single external dep happy place a while longer. I can pull that in if you'd prefer.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Add the dependency ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


return logger
}

module.exports = {
register,
name: 'hapi-pino'
Expand Down