From 5005ea4d0e2db640364e5e03e3eb3c22fca0d2c0 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Sat, 11 Nov 2017 00:11:10 -0500 Subject: [PATCH 1/3] First pass at `ignorePaths` --- index.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/index.js b/index.js index f51d8bf..58d733b 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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 }) @@ -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' From 3288083bec366dff86b64815707d7e8119d48deb Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 13 Nov 2017 10:48:43 -0500 Subject: [PATCH 2/3] Add test coverage `ignorePaths` --- index.js | 2 +- test.js | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 58d733b..35bc1dd 100644 --- a/index.js +++ b/index.js @@ -63,7 +63,7 @@ async function register (server, options) { server.ext('onRequest', (request, h) => { if (options.ignorePaths && ignoreTable[request.url.path]) { request.logger = nullLogger - return h.continue() + return h.continue } request.logger = logger.child({ req: request }) return h.continue diff --git a/test.js b/test.js index d53728c..3103ac7 100644 --- a/test.js +++ b/test.js @@ -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 }) }) @@ -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 + }) +}) From e59ca05f512d467878052a4030840384b3ee96ff Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 15 Nov 2017 09:42:31 -0500 Subject: [PATCH 3/3] Use `abstract-logging` for nullLogger --- index.js | 16 ++-------------- package.json | 1 + 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 35bc1dd..150c213 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ 'use strict' const pino = require('pino') +const nullLogger = require('abstract-logging') const levels = ['trace', 'debug', 'info', 'warn', 'error'] const levelTags = { @@ -45,10 +46,9 @@ 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 } @@ -164,18 +164,6 @@ 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' diff --git a/package.json b/package.json index 8e36024..b602770 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "standard": "^10.0.3" }, "dependencies": { + "abstract-logging": "^1.0.0", "pino": "^4.9.0" }, "repository": {