Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autoLogging.ignore doesn't receive the response object #356

Open
deni64k opened this issue Nov 10, 2024 · 1 comment
Open

autoLogging.ignore doesn't receive the response object #356

deni64k opened this issue Nov 10, 2024 · 1 comment

Comments

@deni64k
Copy link

deni64k commented Nov 10, 2024

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

@gudmojo
Copy link

gudmojo commented Nov 22, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants