pinojs / pino-http

🌲 high-speed HTTP logger for Node.js
MIT License
539 stars 117 forks source link

I can't log in to response to the body #340

Closed 1340145680 closed 3 months ago

1340145680 commented 3 months ago

The base framework I use is express

I modified my code according to the Add log request body example

const httpLogger = pinoHttp({
  level: "trace",
  quietReqLogger: true,
  serializers: {
    req(req) {
      console.log(req);
      req.body = req.raw.body;
      return req;
    },
  },
  })

I get the following data on my console, where the raw data type is IncomingMessage

console image

There is indeed body in raw

raw data view

But I can't get it according to the docs

req(req) {
    console.log("req.raw.body:", req.raw.body);
    req.body = req.raw.body;
    return req;
}

console image

1340145680 commented 3 months ago

@mcollina

jsumners commented 3 months ago

Please provide a minimal reproducible example. Doing so will help us diagnose your issue. It should be the bare minimum code needed to trigger the issue, and easily runnable without any changes or extra code.

You may use a GitHub repository to host the code if it is too much to fit in a code block (or two).

1340145680 commented 3 months ago

Sorry, I got the middleware mixed up.

error code

app.use(logger); //pino logger
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

correct code

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(logger); //pino logger

The logger must be parsed after the body middleware of the incoming request.