Repository: pinojs/hapi-pino
Pull request #18 (2017-07-06 12:19:28)

Add possibility to define custom serializers for req, res and err
Related to #13 

- Add possibility to define custom serializers
- Add option `logPayload` to log request payload


Patch:
diff --git a/README.md b/README.md
index 24e7af4..1ae6ec8 100644
--- a/README.md
+++ b/README.md
@@ -81,7 +81,7 @@ server.register(require('hapi-pino'), (err) => {
 events"](#hapievents) section.
 
 ### Options
-
+- `[logPayload]` – when enabled, add the request payload as `payload` 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 ddba5a8..165f09b 100644
--- a/index.js
+++ b/index.js
@@ -13,9 +13,9 @@ module.exports.levelTags = {
 
 function register (server, options, next) {
   options.serializers = options.serializers || {}
-  options.serializers.req = asReqValue
-  options.serializers.res = pino.stdSerializers.res
-  options.serializers.err = pino.stdSerializers.err
+  options.serializers.req = options.serializers.req || asReqValue
+  options.serializers.res = options.serializers.res || pino.stdSerializers.res
+  options.serializers.err = options.serializers.err || pino.stdSerializers.err
 
   if (options.logEvents === undefined) {
     options.logEvents = ['onPostStart', 'onPostStop', 'response', 'request-error']
@@ -81,6 +81,7 @@ function register (server, options, next) {
   tryAddEvent(server, options, 'on', 'response', function (request) {
     const info = request.info
     request.logger.info({
+      payload: options.logPayload ? request.payload : undefined,
       res: request.raw.res,
       responseTime: info.responded - info.received
     }, 'request completed')


Test patch:
diff --git a/test.js b/test.js
index 63be3b0..21557b6 100644
--- a/test.js
+++ b/test.js
@@ -20,6 +20,19 @@ const Pino = require('.')
 function getServer () {
   const server = new Hapi.Server()
   server.connection({ port: 3000 })
+  server.route([
+    {
+      method: 'POST',
+      path: '/',
+      handler: (request, reply) => reply('ok')
+    },
+    {
+      method: 'GET',
+      path: '/error',
+      handler: (request, reply) => reply(new Error('foobar'))
+    }
+  ])
+
   return server
 }
 
@@ -601,3 +614,112 @@ experiment('logging with mergeHapiLogData option enabled', () => {
     })
   })
 })
+
+experiment('logging with overridden serializer', () => {
+  test('with pre-defined req serializer', (done) => {
+    const server = getServer()
+    const stream = sink((data) => {
+      expect(data.req.uri).to.equal('/')
+      done()
+    })
+    const logger = require('pino')(stream)
+    const plugin = {
+      register: Pino.register,
+      options: {
+        instance: logger,
+        serializers: {
+          req: (req) => ({ uri: req.raw.req.url })
+        }
+      }
+    }
+
+    server.register(plugin, (err) => {
+      expect(err).to.be.undefined()
+      server.inject({
+        method: 'GET',
+        url: '/'
+      })
+    })
+  })
+
+  test('with pre-defined res serializer', (done) => {
+    const server = getServer()
+    const stream = sink((data) => {
+      expect(data.res.code).to.equal(404)
+      done()
+    })
+    const logger = require('pino')(stream)
+    const plugin = {
+      register: Pino.register,
+      options: {
+        instance: logger,
+        serializers: {
+          res: (res) => ({ code: res.statusCode })
+        }
+      }
+    }
+
+    server.register(plugin, (err) => {
+      expect(err).to.be.undefined()
+      server.inject({
+        method: 'GET',
+        url: '/'
+      })
+    })
+  })
+
+  test('with pre-defined err serializer', (done) => {
+    const server = getServer()
+    const stream = sink((data) => {
+      expect(data.err.errStack).to.not.be.undefined()
+      done()
+    })
+    const logger = require('pino')(stream)
+    const plugin = {
+      register: Pino.register,
+      options: {
+        instance: logger,
+        serializers: {
+          err: (err) => ({ errStack: err.stack })
+        }
+      }
+    }
+
+    server.register(plugin, (err) => {
+      expect(err).to.be.undefined()
+      server.inject({
+        method: 'GET',
+        url: '/error'
+      })
+    })
+  })
+})
+
+experiment('logging with request payload', () => {
+  test('with pre-defined req serializer', (done) => {
+    const server = getServer()
+    const stream = sink((data) => {
+      expect(data.payload).to.equal({ foo: 42 })
+      done()
+    })
+    const logger = require('pino')(stream)
+    const plugin = {
+      register: Pino.register,
+      options: {
+        instance: logger,
+        logPayload: true
+      }
+    }
+
+    server.register(plugin, (err) => {
+      expect(err).to.be.undefined()
+      server.inject({
+        method: 'POST',
+        url: '/',
+        payload: {
+          foo: 42
+        }
+      })
+    })
+  })
+})