Repository: pinojs/hapi-pino
Pull request #35 (2017-11-26 02:53:57)

feat(route-tags): logs route tags if options.logRouteTags is true
This allows the tags in hapi `route.options.tags` to be part of the logged data. This is useful to e.g. classify the requests in a dashboard.

two tests are added testing for the correct behaviour in both cases `logRouteTags: true` and the absence of this option property.


Patch:
diff --git a/README.md b/README.md
index a22e1da..50945dd 100644
--- a/README.md
+++ b/README.md
@@ -81,6 +81,7 @@ events"](#hapievents) section.
 
 ### Options
 - `[logPayload]` – when enabled, add the request payload as `payload` to the `response` event log. Defaults to `false`.
+- `[logRouteTags]` – when enabled, add the request route tags (as configured in hapi `route.options.tags`) `tags` to the `response` event log. Defaults to `false`.
 - `[stream]` - the binary stream to write stuff to, defaults to
   `process.stdout`.
 - `[prettyPrint]` - pretty print the logs (same as `node server |
diff --git a/index.js b/index.js
index 1d76f4b..9f9c009 100644
--- a/index.js
+++ b/index.js
@@ -99,6 +99,7 @@ async function register (server, options) {
     const info = request.info
     request.logger.info({
       payload: options.logPayload ? request.payload : undefined,
+      tags: options.logRouteTags ? request.route.settings.tags : undefined,
       res: request.raw.res,
       responseTime: info.responded - info.received
     }, 'request completed')


Test patch:
diff --git a/test.js b/test.js
index 422af45..c279dbc 100644
--- a/test.js
+++ b/test.js
@@ -25,6 +25,9 @@ function getServer () {
     {
       method: 'GET',
       path: '/something',
+      options: {
+        tags: ['foo']
+      },
       handler: async (request, h) => 'ok'
     },
     {
@@ -883,3 +886,62 @@ experiment('ignore request logs for paths in ignorePaths', () => {
     await done
   })
 })
+
+experiment('logging with logRouteTags option enabled', () => {
+  test('when logRouteTags is true, tags are part of the logged object', async () => {
+    const server = getServer()
+    let resolver
+    const done = new Promise((resolve, reject) => {
+      resolver = resolve
+    })
+    const stream = sink((data) => {
+      expect(data.tags[0]).to.be.equal('foo')
+      resolver()
+    })
+    const logger = require('pino')(stream)
+    const plugin = {
+      plugin: Pino,
+      options: {
+        instance: logger,
+        logRouteTags: true
+      }
+    }
+
+    await server.register(plugin)
+
+    await server.inject({
+      method: 'GET',
+      url: '/something'
+    })
+
+    await done
+  })
+
+  test('when logRouteTags is not true, tags are not part of the logged object', async () => {
+    const server = getServer()
+    let resolver
+    const done = new Promise((resolve, reject) => {
+      resolver = resolve
+    })
+    const stream = sink((data) => {
+      expect(data.tags).to.be.undefined()
+      resolver()
+    })
+    const logger = require('pino')(stream)
+    const plugin = {
+      plugin: Pino,
+      options: {
+        instance: logger
+      }
+    }
+
+    await server.register(plugin)
+
+    await server.inject({
+      method: 'GET',
+      url: '/something'
+    })
+
+    await done
+  })
+})