Implement Sentry Logging Into Node.js App

Sentry Logging For Nodejs App In Express

Sentry Logging For Nodejs App In Express

ade great use of Sentry for it’s unhandled error tracking as well as it’s capabilities for capturing custom messages. Here is a simple implementation to get Sentry up and running in a Node.js app running on Express.

const { sentry } = config;
if (sentry) {
  if (sentry.dsn) {
    // Configure Raven
    Raven.config(sentry.dsn).install();
    // The request handler must be the first middleware on the app
    app.use(Raven.requestHandler());
    // The error handler must be before any other error middleware
    app.use(Raven.errorHandler());
  }
}

We opt to store our dsn values in a config file specific to environment.

An example of a config file might look like (Values found in your Sentry Account):

module.exports = {
  env: 'dev',
  http: 8080,
  sentry: {
    clientDsn: 'https://1111111@sentry.io/22222',
    dsn: 'https://11111:2222222@sentry.io/33333333'
  }
};

This should be done as early in the process as possible, essentially as soon as you initialize

app

you should be initializing Sentry to ensure you capture exceptions at all levels.

Reference: Sentry Implementation Notes

Leave a Reply

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

StackOverflow Profile