diff --git a/README.md b/README.md index 227d734..a29321b 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,12 @@ events"](#hapievents) section. When enabled, add the request query as `queryParams` to the `response` event log. +### `options.logPathParams: boolean` + + **Default**: `false` + + When enabled, add the request params as `pathParams` to the `response` event log. + ### `options.logRouteTags: boolean` **Default**: `false` diff --git a/index.d.ts b/index.d.ts index ad0e5eb..e94deb1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -18,6 +18,7 @@ declare namespace HapiPino { interface Options extends pino.LoggerOptions { timestamp?: boolean | (() => string) | undefined; logQueryParams?: boolean | undefined; + logPathParams?: boolean | undefined; logPayload?: boolean | undefined; logRouteTags?: boolean | undefined; logRequestStart?: boolean | ((req: Request) => boolean) | undefined; diff --git a/index.js b/index.js index 6e54cbe..225e875 100644 --- a/index.js +++ b/index.js @@ -177,6 +177,7 @@ async function register (server, options) { { payload: options.logPayload ? request.payload : undefined, queryParams: options.logQueryParams ? request.query : undefined, + pathParams: options.logPathParams ? request.params : undefined, tags: options.logRouteTags ? request.route.settings.tags : undefined, res: request.raw.res, responseTime diff --git a/index.test-d.ts b/index.test-d.ts index 2612435..6d374cf 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -10,6 +10,7 @@ const server = new Server(); const options: HapiPino.Options = { timestamp: () => `,"time":"${new Date(Date.now()).toISOString()}"`, logQueryParams: false, + logPathParams: false, logPayload: false, logRouteTags: false, logRequestStart: false, diff --git a/test.js b/test.js index 1737429..b0daef5 100644 --- a/test.js +++ b/test.js @@ -36,6 +36,11 @@ function getServer () { path: '/', handler: async (request, h) => 'ok' }, + { + method: 'POST', + path: '/{foo}-{bar}', + handler: async (request, h) => 'ok' + }, { method: 'GET', path: '/error', @@ -2405,3 +2410,34 @@ experiment('logging with request queryParams', () => { await done }) }) + +experiment('logging with request pathParams', () => { + test('with pre-defined req serializer', async () => { + const server = getServer() + let resolver + const done = new Promise((resolve, reject) => { + resolver = resolve + }) + const stream = sink(data => { + expect(data.pathParams).to.equal({ foo: '42', bar: '43' }) + resolver() + }) + const logger = require('pino')(stream) + const plugin = { + plugin: Pino, + options: { + instance: logger, + logPathParams: true + } + } + + await server.register(plugin) + + await server.inject({ + method: 'POST', + url: '/42-43' + }) + + await done + }) +})