Skip to content

Commit

Permalink
First pass at ignorePaths
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Williams committed Nov 11, 2017
1 parent 38b7d90 commit 5005ea4
Showing 1 changed file with 24 additions and 0 deletions.
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
}

return logger
}

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

0 comments on commit 5005ea4

Please sign in to comment.