pinojs / koa-pino-logger

🌲 pino logging koa middleware
MIT License
94 stars 27 forks source link

Add support to have access to koa request body #21

Open ivansantosz opened 5 years ago

ivansantosz commented 5 years ago

Right now it only pass ctx.req down to pino http.

Things like ctx.request.body are left out.

Was that intentional?

MikuZZZ commented 4 years ago

I know it's not the perfect way, but I use the following code to solve this problem.

app.use(async (ctx, next) => {
  ctx.req.body = ctx.request.body;
  await next();
});

app.use(
  logger({
    serializers: {
      err: pino.stdSerializers.err,
      req: (req) => {
        req.body = req.raw.body;
        return req;
      },
      res: pino.stdSerializers.res,
    },
  }),
);