[Sentry] Using Sentry for tracking error log

ยท

2 min read

[Sentry] Using Sentry for tracking error log

๐Ÿ“Œ What is Sentry?

  • Sentry is an external monitoring and error logging service that helps identify and categorize errors in code.

  • By using errorHandler in the code, you can identify specific error codes (e.g., 404, 500) as errors.

  • You can view error log information on the Sentry website, and the integration between our code and the website is done through the DSN (Data Source Name) address.

  • Since DSN information is treated as confidential, it should be stored in a .env file or similar.

๐Ÿ“Œ How to Use Sentry Website

0. Sign Up

1. Create a New Project

2. Default Alert Configuration

  1. I'll create my own alerts later - Do not set up any alerts for now.

  2. Alert me on every new issue - Set up an alert that notifies you whenever a new issue is reported.

  3. When there are more than ... - Set up an alert that triggers when a unique error occurs more than m times in n minutes.

3. Set Project Name and Team

4. Save the DSN

After creating the project, you can obtain the DSN for the project from the Installation Settings guide page.


๐Ÿ“Œ Using Sentry in Node.js

1. Install the Sentry Library

npm i --save @sentry/node

2. Set Up Sentry in app.js

jsCopy codeconst Sentry = require("@sentry/node");

// The initialization and requestHandler should be placed at the top of other module or middleware code.
Sentry.init({
  dsn: process.env.DSN, // Use your own DSN.
  tracesSampleRate: 1.0,
});
app.use(Sentry.Handlers.requestHandler());

// Other module and middleware code

// The Sentry error handler should be placed before other error handler middleware.
app.use(Sentry.Handlers.errorHandler());

// Other error handling middleware code

3. Customize Sentry Error Events

You can customize error handling within Sentry.Handlers.errorHandler().

jsCopy codeSentry.Handlers.errorHandler({
    shouldHandleError(error) {
      if (
        error.errorCode === 403 ||
        error.errorCode === 404 ||
        error.errorCode === 500
      ) {
        return true;
      }
      return false;
    }

The provided code triggers error events only for the 403, 404, and 500 errors. The event is sent to the Sentry project when the shouldHandleError() function returns true.

For more detailed configuration options, refer to the official documentation.

๐Ÿ“Œ Exploring Sentry Projects

You can see the accumulated issue events as shown above.

You can share error logs with team members!

ย