sentry
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSentry Integration
Sentry集成
Guidelines for using Sentry for error monitoring and performance tracing.
在Next.js应用中使用Sentry进行错误监控和性能追踪的指南。
Exception Catching
异常捕获
Use in try/catch blocks:
Sentry.captureException(error)javascript
try {
await riskyOperation();
} catch (error) {
Sentry.captureException(error);
throw error;
}在try/catch块中使用:
Sentry.captureException(error)javascript
try {
await riskyOperation();
} catch (error) {
Sentry.captureException(error);
throw error;
}Performance Tracing
性能追踪
Create spans for meaningful actions like button clicks, API calls, and function calls.
为有意义的操作(如按钮点击、API调用和函数调用)创建span。
UI Actions
UI操作
javascript
function handleClick() {
Sentry.startSpan(
{ op: "ui.click", name: "Submit Form" },
(span) => {
span.setAttribute("formId", formId);
submitForm();
}
);
}javascript
function handleClick() {
Sentry.startSpan(
{ op: "ui.click", name: "Submit Form" },
(span) => {
span.setAttribute("formId", formId);
submitForm();
}
);
}API Calls
API调用
javascript
async function fetchData(id) {
return Sentry.startSpan(
{ op: "http.client", name: `GET /api/items/${id}` },
async () => {
const response = await fetch(`/api/items/${id}`);
return response.json();
}
);
}javascript
async function fetchData(id) {
return Sentry.startSpan(
{ op: "http.client", name: `GET /api/items/${id}` },
async () => {
const response = await fetch(`/api/items/${id}`);
return response.json();
}
);
}Configuration (Next.js)
配置(Next.js)
Sentry initialization files:
- - Client-side
sentry.client.config.ts - - Server-side
sentry.server.config.ts - - Edge runtime
sentry.edge.config.ts
Import with - no need to initialize in other files.
import * as Sentry from "@sentry/nextjs"Sentry初始化文件:
- - 客户端
sentry.client.config.ts - - 服务端
sentry.server.config.ts - - 边缘运行时
sentry.edge.config.ts
使用导入,无需在其他文件中初始化。
import * as Sentry from "@sentry/nextjs"Basic Setup
基础配置
javascript
import * as Sentry from "@sentry/nextjs";
Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
enableLogs: true,
});javascript
import * as Sentry from "@sentry/nextjs";
Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
enableLogs: true,
});With Console Logging
包含控制台日志
javascript
Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
integrations: [
Sentry.consoleLoggingIntegration({ levels: ["log", "warn", "error"] }),
],
});javascript
Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
integrations: [
Sentry.consoleLoggingIntegration({ levels: ["log", "warn", "error"] }),
],
});Structured Logging
结构化日志
Use for template literals with variables:
logger.fmtjavascript
const { logger } = Sentry;
logger.trace("Starting connection", { database: "users" });
logger.debug(logger.fmt`Cache miss for: ${userId}`);
logger.info("Updated profile", { profileId: 345 });
logger.warn("Rate limit reached", { endpoint: "/api/data" });
logger.error("Payment failed", { orderId: "order_123" });
logger.fatal("Connection pool exhausted", { activeConnections: 100 });使用处理带变量的模板字符串:
logger.fmtjavascript
const { logger } = Sentry;
logger.trace("Starting connection", { database: "users" });
logger.debug(logger.fmt`Cache miss for: ${userId}`);
logger.info("Updated profile", { profileId: 345 });
logger.warn("Rate limit reached", { endpoint: "/api/data" });
logger.error("Payment failed", { orderId: "order_123" });
logger.fatal("Connection pool exhausted", { activeConnections: 100 });