Repository: pinojs/hapi-pino
Pull request #81 (2019-07-27 18:45:15)

Fix UnhandledPromiseRejection warning when logging on an ignored path
# Description
A few days ago I ran into a bug where logging via `request.log` would cause unhandled promise rejection warnings on routes with disabled logging. I traced it down to [this line](https://github.com/pinojs/hapi-pino/blob/master/index.js#L172). Basically on an ignored route, `current` is equal to the `nullLogger` which doesn't have a `levels` property. 

# Steps to reproduce
- Add a `request.log([level])` statement within a route handler such that `level` is a valid log level configured within hapi-pino such as 'info', 'warn', 'error', etc.
- Disable that route by adding it to `ignorePaths`
- Make a request to that route

I've added a unit test which reproduces this bug.

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

Coverage increased (+0.05%) to 98.861% when pulling **b8549d0fcfce4086ffbade74229969e942d9bc2d on qswitcher:ignore-path-fix** into **79ad1b4612ffb0b7b1c99174bdc947f0d8dd83dd on pinojs:master**.

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

Coverage increased (+0.05%) to 98.861% when pulling **b8549d0fcfce4086ffbade74229969e942d9bc2d on qswitcher:ignore-path-fix** into **79ad1b4612ffb0b7b1c99174bdc947f0d8dd83dd on pinojs:master**.


Patch:
diff --git a/index.js b/index.js
index 3dec151..cbe723a 100644
--- a/index.js
+++ b/index.js
@@ -145,6 +145,11 @@ async function register (server, options) {
   }
 
   function logEvent (current, event) {
+    // check for null logger
+    if (current === nullLogger) {
+      return
+    }
+
     var tags = event.tags
     var data = event.data
 


Test patch:
diff --git a/test.js b/test.js
index db1cdd4..e0269df 100644
--- a/test.js
+++ b/test.js
@@ -1067,6 +1067,64 @@ experiment('ignore response logs for paths in ignorePaths', () => {
   })
 })
 
+experiment('ignore request.log logs for paths in ignorePaths', () => {
+  test('when path matches entry in ignorePaths, nothing should be logged', async () => {
+    const level = 'info'
+    const server = getServer()
+    server.route({
+      path: '/ignored',
+      method: 'GET',
+      handler: (req, h) => {
+        req.log([level], 'hello logger')
+        return 'hello world'
+      }
+    })
+
+    server.route({
+      path: '/foo',
+      method: 'GET',
+      handler: (req, h) => {
+        req.log([level], 'foo')
+        return 'foo'
+      }
+    })
+
+    let resolver
+    const done = new Promise((resolve, reject) => {
+      resolver = resolve
+    })
+    const stream = sink((data) => {
+      expect(data.req.url).to.endWith('/foo')
+      expect(data.tags).to.equal([level])
+      expect(data.data).to.equal('foo')
+      resolver()
+    })
+    const logger = require('pino')(stream)
+    const plugin = {
+      plugin: Pino,
+      options: {
+        instance: logger,
+        logEvents: ['request-error'],
+        ignorePaths: ['/ignored']
+      }
+    }
+
+    await server.register(plugin)
+
+    await server.inject({
+      method: 'GET',
+      url: '/ignored'
+    })
+
+    await server.inject({
+      method: 'GET',
+      url: '/foo'
+
+    })
+    await done
+  })
+})
+
 experiment('logging with logRouteTags option enabled', () => {
   test('when logRouteTags is true, tags are part of the logged object', async () => {
     const server = getServer()