Skip to content

Question:How to get the response body in hapi-pino logs? #121

@veerugp

Description

@veerugp

My code is a follows:

'use strict';

const hapi = require('@hapi/hapi')
const HapiPino = require('hapi-pino')

const server = hapi.server({ port: 3000 })

server.route([
  {
    method: 'POST',
    path: '/response',
    handler:(request, h) => {
      return {'test':'Resp Msg'}
    }
  },
]);

function resSerializer(res) {
  return {
      body:res.raw.body
  };
}

(async () => {
  try {
    await server.register({
      plugin: HapiPino,
      options: {
        logPayload: true,
        serializers:{
          res: resSerializer
        },       
      }
    })
    await server.start()
    console.log('Server started successfully')
  } catch (err) {
    console.error(err)
  }
})()

Logs are as shown as below:

{"level":30,"time":1598811397374,"pid":15504,"hostname":"IN5CG7192BNG","req":{"id":"1598811397349:IN5CG7192BNG:15504:kehf0ria:10000","method":"post","url":"/response","headers":{"content-type":"application/json","user-agent":"PostmanRuntime/7.6.0","accept":"*/*","host":"localhost:3000","accept-encoding":"gzip, deflate","content-length":"50","connection":"keep-alive"},"remoteAddress":"127.0.0.1","remotePort":61801},"payload":{"test1":"JVA","test2":"JVA","test3":"JVA"},"req":{"id":"1598811397349:IN5CG7192BNG:15504:kehf0ria:10000","method":"post","url":"/response","headers":{"content-type":"application/json","user-agent":"PostmanRuntime/7.6.0","accept":"*/*","host":"localhost:3000","accept-encoding":"gzip, deflate","content-length":"50","connection":"keep-alive"},"remoteAddress":"127.0.0.1","remotePort":61801},"res":{},"responseTime":24,"msg":"request completed"}

Response body is showing as "res":{}

How to get the response body value, which is {'test':'Resp Msg'}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions