pino-http icon indicating copy to clipboard operation
pino-http copied to clipboard

[QUESTION] logging response body

Open kundkingan opened this issue 1 year ago • 3 comments

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 avatar Mar 29 '23 13:03 kundkingan

@kundkingan did you figure this out?

maxckelly avatar Apr 24 '23 06:04 maxckelly

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();
};

DmitriyMolochkov avatar Jul 11 '23 23:07 DmitriyMolochkov

@kundkingan did you figure this out?

Haven't tried it out yet!

kundkingan avatar Sep 14 '23 11:09 kundkingan