Repository: pinojs/hapi-pino
Pull request #51 (2018-03-26 20:09:47)

Backport 42 to v2.x.x
cc @benib 


Patch:
diff --git a/README.md b/README.md
index d30311b..64d356c 100644
--- a/README.md
+++ b/README.md
@@ -92,6 +92,14 @@ events"](#hapievents) section.
   mappings. The default level tags are exposed via `hapi-pino.levelTags`.
 - `[allTags]` - the logging level to apply to all tags not matched by
   `tags`, defaults to `'info'`.
+ `[serializers]` - an object to overwrite the default serializers. You can but don't have to overwrite all of them. E.g. to redact the authorization header in the logs:
+   ```
+    {
+      req: require('pino-noir')(['req.headers.authorization']).req
+      res: ...
+      err: ...
+    }
+   ```
 - `[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
diff --git a/index.js b/index.js
index 27894bb..eddb3c8 100644
--- a/index.js
+++ b/index.js
@@ -1,6 +1,7 @@
 'use strict'
 
 const pino = require('pino')
+const stdSerializers = pino.stdSerializers
 const nullLogger = require('abstract-logging')
 
 const levels = ['trace', 'debug', 'info', 'warn', 'error']
@@ -14,8 +15,8 @@ module.exports.levelTags = {
 
 function register (server, options, next) {
   options.serializers = options.serializers || {}
-  options.serializers.req = options.serializers.req || asReqValue
-  options.serializers.res = options.serializers.res || pino.stdSerializers.res
+  options.serializers.req = stdSerializers.wrapRequestSerializer(options.serializers.req || stdSerializers.req)
+  options.serializers.res = stdSerializers.wrapResponseSerializer(options.serializers.res || stdSerializers.res)
   options.serializers.err = options.serializers.err || pino.stdSerializers.err
 
   if (options.logEvents === undefined) {
@@ -141,18 +142,6 @@ function register (server, options, next) {
   }
 }
 
-function asReqValue (req) {
-  const raw = req.raw.req
-  return {
-    id: req.id,
-    method: raw.method,
-    url: raw.url,
-    headers: raw.headers,
-    remoteAddress: raw.connection.remoteAddress,
-    remotePort: raw.connection.remotePort
-  }
-}
-
 module.exports.register = register
 module.exports.register.attributes = {
   pkg: require('./package')
diff --git a/package.json b/package.json
index 7d4a137..1883390 100644
--- a/package.json
+++ b/package.json
@@ -31,7 +31,7 @@
   },
   "dependencies": {
     "abstract-logging": "^1.0.0",
-    "pino": "^4.10.2"
+    "pino": "^4.15.2"
   },
   "repository": {
     "type": "git",


Test patch:
diff --git a/test.js b/test.js
index ab2a35b..0da62a5 100644
--- a/test.js
+++ b/test.js
@@ -613,8 +613,8 @@ experiment('logging with mergeHapiLogData option enabled', () => {
   })
 })
 
-experiment('logging with overridden serializer', () => {
-  test('with pre-defined req serializer', (done) => {
+experiment('custom serializers', () => {
+  test('logging with configured req serializer', (done) => {
     const server = getServer()
     const stream = sink((data) => {
       expect(data.req.uri).to.equal('/')
@@ -640,7 +640,7 @@ experiment('logging with overridden serializer', () => {
     })
   })
 
-  test('with pre-defined res serializer', (done) => {
+  test('logging with configured res serializer', (done) => {
     const server = getServer()
     const stream = sink((data) => {
       expect(data.res.code).to.equal(404)
@@ -652,7 +652,7 @@ experiment('logging with overridden serializer', () => {
       options: {
         instance: logger,
         serializers: {
-          res: (res) => ({ code: res.statusCode })
+          res: (res) => ({ code: res.statusCode, raw: res.raw })
         }
       }
     }
@@ -666,7 +666,7 @@ experiment('logging with overridden serializer', () => {
     })
   })
 
-  test('with pre-defined err serializer', (done) => {
+  test('logging with pre-defined err serializer', (done) => {
     const server = getServer()
     const stream = sink((data) => {
       expect(data.err.errStack).to.not.be.undefined()
@@ -691,6 +691,66 @@ experiment('logging with overridden serializer', () => {
       })
     })
   })
+
+  test('req.raw is not enumerable', (done) => {
+    const server = getServer()
+    const stream = sink((data) => {
+      expect(data.req).to.be.an.object()
+      done()
+    })
+    const logger = require('pino')(stream)
+    const plugin = {
+      register: Pino.register,
+      options: {
+        instance: logger,
+        serializers: {
+          req: (req) => {
+            expect(req.raw).to.be.an.object()
+            expect(req.propertyIsEnumerable('raw')).to.be.false()
+            return req
+          }
+        }
+      }
+    }
+
+    server.register(plugin, (err) => {
+      expect(err).to.be.undefined()
+      server.inject({
+        method: 'GET',
+        url: '/'
+      })
+    })
+  })
+
+  test('res.raw is not enumerable', (done) => {
+    const server = getServer()
+    const stream = sink((data) => {
+      expect(data.res).to.be.an.object()
+      done()
+    })
+    const logger = require('pino')(stream)
+    const plugin = {
+      register: Pino.register,
+      options: {
+        instance: logger,
+        serializers: {
+          res: (res) => {
+            expect(res.raw).to.be.an.object()
+            expect(res.propertyIsEnumerable('raw')).to.be.false()
+            return res
+          }
+        }
+      }
+    }
+
+    server.register(plugin, (err) => {
+      expect(err).to.be.undefined()
+      server.inject({
+        method: 'GET',
+        url: '/'
+      })
+    })
+  })
 })
 
 experiment('logging with request payload', () => {