Repository: pinojs/hapi-pino
Pull request #164 (2022-05-26 16:24:51)

Add optional support to log path params
hapi-pino binds the request only once during the `onRequest` hook:

https://github.com/pinojs/hapi-pino/blob/27806507920fda5db27c2daca0a99259423ba647/index.js#L111-L118

However, hapi only makes `request.params` available later in the [request lifecycle](https://hapi.dev/api/?v=20.2.2#request-lifecycle), and it is accessible only after `onPreAuth` at the earliest. This pull request adds optional support to log `pathParams` when `logPathParams` is `true`, in pretty much the same way that `logQueryParams` works

coveralls: 
[![Coverage Status](https://coveralls.io/builds/49488728/badge)](https://coveralls.io/builds/49488728)

Coverage decreased (-99.07%) to 0.0% when pulling **27b275144b3698168f20023eb3f51b505b60c1bd on jonathansamines:feature/optional-path-params-log** into **27806507920fda5db27c2daca0a99259423ba647 on pinojs:master**.


Patch:
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


Test patch:
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
+  })
+})