Categories
JavaScript Answers

How to log response body with Node Express?

Spread the love

To log response body with Node Express, we use express-winston.

For instance, we write

expressWinston.requestWhitelist.push("body");
expressWinston.responseWhitelist.push("body");
app.use(
  expressWinston.logger({
    transports: [
      new winston.transports.Console({
        json: true,
        colorize: true,
      }),
    ],
    meta: true,
    msg: "HTTP {{req.method}} {{req.url}}",
    expressFormat: true,
    colorStatus: true,
    ignoreRoute: (req, res) => {
      return false;
    },
  })
);

to add a logger with expressWinston.logger.

The meta option lets us set whether to log meta data.

msg is the log entry format.

expressFormat lets us set whether to use the default formatting.

colorStatus lets us set whether to color the log entry.

ignoreRoute lets us set the routes to skip logging for.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *