Error Log Management and Incident Detection Enhancement Guide Using Sentry
Introduction
In web application development, proper management of error logs is indispensable. Especially in production environments, it is crucial to detect errors quickly and respond promptly.
This article organizes methods for strengthening error log management and incident detection using Sentry, targeting web applications built with Next.js and Express.
Importance of Error Logs
By properly organizing error logs, you can gain the following benefits:
- Early detection of incidents: Errors can be detected immediately when they occur.
- Improved reproducibility of issues: By recording detailed logs, developers can accurately understand the problem.
- Minimizing user impact: You can respond to incidents quickly and reduce the impact on users.
- More efficient development: Leveraging error logs reduces the time required for debugging.
Overview of Sentry
Sentry is a tool that visualizes application error logs and performance, and notifies you of incidents in real time. It has the following features:
- Real-time error tracking: Receive notifications immediately when an error occurs.
- Stack trace information: Check the line of code and stack trace where the error occurred.
- Visualization of user impact: Understand which users were affected and to what extent.
- Support for various platforms: Available in a wide range of environments such as JavaScript, Node.js, React, Next.js, and Express.
- Integration with external tools: Can be integrated with Slack, Teams, GitHub, JIRA, and more.
Recently, the following new features have also been added:
- Session Replay: Reproduces the user’s screen operations to efficiently investigate bugs with low reproducibility.
- AI Assist: Suggests possible causes and fixes based on stack traces.
Steps to Introduce Sentry
1. Create a Sentry Account
First, create an account on the official Sentry site and set up a new project.
2. Install the Sentry SDK
For example, to introduce Sentry into a Next.js application, you can set it up with the following steps:
npm install @sentry/nextjs
3. Add to the Configuration File
Add Sentry settings to next.config.js.
const { withSentryConfig } = require('@sentry/nextjs');
const moduleExports = {
sentry: {
dsn: process.env.SENTRY_DSN, // Manage Sentry DSN via environment variables
tracesSampleRate: 1.0, // Sampling rate for performance monitoring
},
};
module.exports = withSentryConfig(moduleExports);
4. Initialization
Add the following initialization process to pages/_app.js or server.ts.
import * as Sentry from '@sentry/nextjs';
Sentry.init({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 1.0,
});
5. Capturing Errors
When capturing errors manually:
try {
throw new Error('Intentional error');
} catch (error) {
Sentry.captureException(error);
}
Example usage in an API handler:
export default async function handler(req, res) {
try {
throw new Error('API error');
} catch (error) {
Sentry.captureException(error);
res.status(500).json({ message: 'An internal error has occurred' });
}
}
Strengthening Incident Detection
To strengthen incident detection using Sentry, keep the following points in mind.
1. Optimize Notification Settings
-
Integration with Slack
- Integrate Slack from Sentry’s “Integrations” so that important errors are notified immediately.
- Example: Use
@hereto notify the team right away.
-
Integration with GitHub Issues
- Automatically create bug reports as GitHub Issues so developers can respond quickly.
2. Use Error Filtering
- Ignore specific errors
- For example, exclude 404 errors caused by bot access from notifications.
Sentry.init({
beforeSend(event) {
if (event.exception && event.exception.values[0].type === 'NotFoundError') {
return null; // Ignore 404 errors
}
return event;
},
});
Operational tips:
- Exclusion rules should not be “set and forget”; review them regularly.
- Continue auditing, as low-priority errors can sometimes be “actually serious.”
3. Release and Version Management
- Send release information to Sentry for each deployment to visualize in which version a problem occurred.
sentry-cli releases new my-app@1.0.0
sentry-cli releases finalize my-app@1.0.0
4. Introduce Performance Monitoring
- Sentry can track not only error detection but also API response times and page load times.
- Adjust
tracesSampleRateto collect appropriate data.
Sentry.init({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 0.2, // 20% sampling rate
});
Operational tips:
- Setting it to 1.0 from the start can cause costs to spike, so it’s safer to begin around 0.1–0.2.
- Use separate DSNs for production and staging to prevent unnecessary data from being mixed.
Conclusion
By properly organizing error logs and leveraging Sentry, you can improve the accuracy of incident detection.
- Introducing Sentry enables real-time error detection and notification.
- Optimize notification settings and filtering to reduce noise.
- Use performance monitoring as well to help improve user experience.
Within a few weeks of introduction, the situation where "we only notice issues when users contact us" was resolved, and the agent of error detection shifted from people to systems. As a result, the development team can focus on new feature development with greater peace of mind.
Use this article as a reference to build robust error log management using Sentry.
Questions about this article 📝
If you have any questions or feedback about the content, please feel free to contact us.Go to inquiry form
Related Articles
Robust Authorization Design for GraphQL and REST APIs: Best Practices for RBAC, ABAC, and OAuth 2.0
2024/05/13Introduction to Automating Development Work: A Complete Guide to ETL (Python), Bots (Slack/Discord), CI/CD (GitHub Actions), and Monitoring (Sentry/Datadog)
2024/02/12How to Easily Build a Web API with Express and MongoDB [TypeScript Compatible]
2024/12/09Express (+ TypeScript) Beginner’s Guide: How to Quickly Build Web Applications
2024/12/07Complete Guide to Refactoring React: Improve Your Code with Modularization, Render Optimization, and Design Patterns
2025/01/13Test Automation with Jest and TypeScript: A Complete Guide from Basic Setup to Writing Type-Safe Tests
2023/09/13ESLint / Prettier Introduction Guide: Thorough Explanation from Husky, CI/CD Integration, to Visualizing Code Quality
2024/02/12Building a Mock Server for Frontend Development: A Practical Guide Using @graphql-tools/mock and Faker
2024/12/30