We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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":{}
"res":{}
How to get the response body value, which is {'test':'Resp Msg'}
{'test':'Resp Msg'}
The text was updated successfully, but these errors were encountered:
The docs say this about the logPayload option:
logPayload
When enabled, add the request payload as payload to the response event log.
So it seems to be to be intended that only the request payload is logged, not the response.
However I'd also like to be able to log the response payload for troubleshooting purposes, there doesn't seem to be an option for this.
Sorry, something went wrong.
You can do this with a formatter:
import _ from "lodash"; const plugin = { plugin: require("hapi-pino"), options: { formatters: { log(object: any) { const body = _.get(object, "req.response.source"); if (body) { return { ...object, body, }; } else { return object; } } } }, };
https://getpino.io/#/docs/api?id=formatters-object
very helpful. Thanks
No branches or pull requests
My code is a follows:
Logs are as shown as below:
Response body is showing as
"res":{}
How to get the response body value, which is
{'test':'Resp Msg'}
The text was updated successfully, but these errors were encountered: