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

`autoLogging.ignore` doesn't receive the response object

Open deni64k opened this issue 1 year ago • 2 comments

Why would autoLogging.ignore not receive a response object. I would like to autolog only errored requests.

deni64k avatar Nov 10 '24 04:11 deni64k

You can do it with an error middleware instead of autologging. Here's how I did it:

  const logRequest = pinoHttp({
    level: 'info',
    enabled: true,
    autoLogging: false,
  });

  const app = express();

  app.use(logRequest);

  app.get('/health', (req, res) => {
    res.send('ok');
  });

  // Only works if it's placed after the routing calls such as app.get()
  app.use((err, req, res, next) => {
    req.log.error(err)
    res.status(500).send('Internal Server Error')
  })

  app.listen(PORT, () => {
    log.info(`server listening for requests on http://localhost:${PORT}`);
  });  

gudmojo avatar Nov 22 '24 16:11 gudmojo

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

jsumners avatar Apr 16 '25 12:04 jsumners