Repository: pinojs/hapi-pino
Pull request #17 (2017-05-25 17:13:38)

Allows users to choose which events to log.
This PR adds a `logEvents` plugin-level option. This options allows user to specify the server events that the library should log.

```js
{
    register: 'hapi-pino',
    options: {
        logEvents: ['onPostStart', 'onPostStop', 'response', 'request-error'] // Default value, logs all events.
    }
}
```
Users can disable all logged events by specifying `logEvents: false|null`.


Patch:
diff --git a/README.md b/README.md
index 4f1ee58..24e7af4 100644
--- a/README.md
+++ b/README.md
@@ -95,6 +95,9 @@ events"](#hapievents) section.
   `tags`, defaults to `'info'`.
 - `[instance]` - uses a previously created Pino instance as the logger.
   The instance's `stream` and `serializers` take precedence.
+- `[logEvents]` - Takes an array of strings with the events to log. Default is to
+  log all events e.g. `['onPostStart', 'onPostStop', 'response', 'request-error']`.
+  Set to `false/null` to disable all events.
 - `[mergeHapiLogData]` - When enabled, Hapi-pino will merge the data received
   from Hapi's logging interface (`server.log(tags, data)` or `request.log(tags, data)`)
   into Pino's logged attributes at root level. If data is a string, it will be used as
diff --git a/index.js b/index.js
index 75ee6ca..ddba5a8 100644
--- a/index.js
+++ b/index.js
@@ -17,6 +17,10 @@ function register (server, options, next) {
   options.serializers.res = pino.stdSerializers.res
   options.serializers.err = pino.stdSerializers.err
 
+  if (options.logEvents === undefined) {
+    options.logEvents = ['onPostStart', 'onPostStop', 'response', 'request-error']
+  }
+
   var logger
   if (options.instance) {
     options.instance.serializers = Object.assign(options.serializers, options.instance.serializers)
@@ -66,7 +70,7 @@ function register (server, options, next) {
   })
 
   // log when a request completes with an error
-  server.on('request-error', function (request, err) {
+  tryAddEvent(server, options, 'on', 'request-error', function (request, err) {
     request.logger.warn({
       res: request.raw.res,
       err: err
@@ -74,7 +78,7 @@ function register (server, options, next) {
   })
 
   // log when a request completes
-  server.on('response', function (request) {
+  tryAddEvent(server, options, 'on', 'response', function (request) {
     const info = request.info
     request.logger.info({
       res: request.raw.res,
@@ -82,18 +86,24 @@ function register (server, options, next) {
     }, 'request completed')
   })
 
-  server.ext('onPostStart', function (s, cb) {
+  tryAddEvent(server, options, 'ext', 'onPostStart', function (s, cb) {
     logger.info(server.info, 'server started')
     cb()
   })
 
-  server.ext('onPostStop', function (s, cb) {
+  tryAddEvent(server, options, 'ext', 'onPostStop', function (s, cb) {
     logger.info(server.info, 'server stopped')
     cb()
   })
 
   next()
 
+  function tryAddEvent (server, options, type, event, cb) {
+    if (options.logEvents && options.logEvents.indexOf(event) !== -1) {
+      server[type](event, cb)
+    }
+  }
+
   function logEvent (current, event) {
     var tags = event.tags
     var data = event.data


Test patch:
diff --git a/test.js b/test.js
index 6ad1c97..63be3b0 100644
--- a/test.js
+++ b/test.js
@@ -8,6 +8,10 @@ const writeStream = require('flush-write-stream')
 const lab = exports.lab = Lab.script()
 const experiment = lab.experiment
 const test = lab.test
+const before = lab.before
+const beforeEach = lab.beforeEach
+const after = lab.after
+const afterEach = lab.afterEach
 const expect = Code.expect
 
 const Hapi = require('hapi')
@@ -86,17 +90,33 @@ experiment('logs through the server.logger()', () => {
   })
 })
 
-test('log on server start', (done) => {
-  const server = getServer()
-  registerWithSink(server, 'info', (data, enc, cb) => {
-    expect(data).to.include(server.info)
-    expect(data.msg).to.equal('server started')
+experiment('log on server start', () => {
+  let server
+
+  before((cb) => {
+    server = getServer()
     cb()
-    done()
-  }, (err) => {
-    expect(err).to.be.undefined()
-    server.start((err) => {
+  })
+
+  after((cb) => {
+    server.stop(cb)
+  })
+
+  test('log on server start', (done) => {
+    let executed = false
+    registerWithSink(server, 'info', (data, enc, cb) => {
+      if (!executed) {
+        executed = true
+        expect(data).to.include(server.info)
+        expect(data.msg).to.equal('server started')
+        cb()
+        done()
+      }
+    }, (err) => {
       expect(err).to.be.undefined()
+      server.start((err) => {
+        expect(err).to.be.undefined()
+      })
     })
   })
 })
@@ -366,6 +386,125 @@ experiment('logs through request.log', () => {
   })
 })
 
+experiment('disables log events', () => {
+  let server
+
+  beforeEach((cb) => {
+    server = getServer()
+    cb()
+  })
+
+  afterEach((cb) => {
+    server.stop()
+    cb()
+  })
+
+  test('server-start', (done) => {
+    let called = false
+    const stream = sink(() => {
+      called = true
+    })
+
+    const plugin = {
+      register: Pino.register,
+      options: {
+        stream: stream,
+        level: 'info',
+        logEvents: false
+      }
+    }
+
+    server.register(plugin, (err) => {
+      expect(err).to.be.undefined()
+      server.start((err) => {
+        expect(err).to.be.undefined()
+        expect(called).to.be.false()
+        done()
+      })
+    })
+  })
+
+  test('server-stop', (done) => {
+    let called = false
+    const stream = sink(() => {
+      called = true
+    })
+
+    const plugin = {
+      register: Pino.register,
+      options: {
+        stream: stream,
+        level: 'info',
+        logEvents: false
+      }
+    }
+
+    server.register(plugin, (err) => {
+      expect(err).to.be.undefined()
+      server.stop((err) => {
+        expect(err).to.be.undefined()
+        expect(called).to.be.false()
+        done()
+      })
+    })
+  })
+
+  test('response', (done) => {
+    let called = false
+    const stream = sink(() => {
+      called = true
+    })
+
+    const plugin = {
+      register: Pino.register,
+      options: {
+        stream: stream,
+        level: 'info',
+        logEvents: false
+      }
+    }
+
+    server.register(plugin, (err) => {
+      expect(err).to.be.undefined()
+      server.inject('/', (res) => {
+        expect(called).to.be.false()
+        done()
+      })
+    })
+  })
+
+  test('request-error', (done) => {
+    let called = false
+    const stream = sink(() => {
+      called = true
+    })
+
+    const plugin = {
+      register: Pino.register,
+      options: {
+        stream: stream,
+        level: 'info',
+        logEvents: false
+      }
+    }
+
+    server.register(plugin, (err) => {
+      expect(err).to.be.undefined()
+      server.route({
+        method: 'GET',
+        path: '/',
+        handler: (request, reply) => {
+          return reply(new Error('boom'))
+        }
+      })
+      server.inject('/', (res) => {
+        expect(called).to.be.false()
+        done()
+      })
+    })
+  })
+})
+
 experiment('uses a prior pino instance', () => {
   test('without pre-defined serializers', (done) => {
     const server = getServer()