pino-http
pino-http copied to clipboard
[QUESTION] logging response body
Is it possible to log response body with the request complete log?
Currently in the res serializers, it's not possible to access the returning body to add it to the final log.
@kundkingan did you figure this out?
You can catch the context using a property with a getter
const middleware = (req, res, next) => {
const logger = pinoHttp({
serializers: {
res(res) {
res.body = res.raw.locals.body;
return res;
},
},
})
logger(
req,
Object.defineProperty(res, 'locals', {
get() {
return { body: res.body };
},
enumerable: true,
configurable: true,
}),
)
next();
};
@kundkingan did you figure this out?
Haven't tried it out yet!