sentry-setup-logging
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSetup Sentry Logging
配置Sentry日志记录
Configure Sentry's structured logging feature.
配置Sentry的结构化日志功能。
Invoke This Skill When
何时调用此技能
- User asks to "setup Sentry logging" or "capture logs in Sentry"
- User wants to integrate logging libraries (Pino, Winston, Loguru) with Sentry
- User asks about or
Sentry.loggersentry_sdk.logger
- 用户要求“配置Sentry日志记录”或“在Sentry中捕获日志”
- 用户希望将日志库(Pino、Winston、Loguru)与Sentry集成
- 用户询问或
Sentry.logger相关内容sentry_sdk.logger
Quick Reference
快速参考
| Platform | Min SDK | Enable Flag | Logger API |
|---|---|---|---|
| JavaScript | 9.41.0+ | | |
| Python | 2.35.0+ | | |
| Ruby | 5.24.0+ | | |
| 平台 | 最低SDK版本 | 启用标识 | 日志器API |
|---|---|---|---|
| JavaScript | 9.41.0+ | | |
| Python | 2.35.0+ | | |
| Ruby | 5.24.0+ | | |
JavaScript Setup
JavaScript 配置步骤
1. Verify SDK version
1. 验证SDK版本
bash
grep -E '"@sentry/(nextjs|react|node|browser)"' package.jsonbash
grep -E '"@sentry/(nextjs|react|node|browser)"' package.json2. Enable in Sentry.init()
2. 在Sentry.init()中启用
javascript
Sentry.init({
dsn: "YOUR_DSN",
enableLogs: true,
});javascript
Sentry.init({
dsn: "YOUR_DSN",
enableLogs: true,
});3. Console capture (optional)
3. 捕获控制台日志(可选)
javascript
integrations: [
Sentry.consoleLoggingIntegration({ levels: ["warn", "error"] }),
],javascript
integrations: [
Sentry.consoleLoggingIntegration({ levels: ["warn", "error"] }),
],4. Use structured logging
4. 使用结构化日志
javascript
Sentry.logger.info("User logged in", { userId: "123" });
Sentry.logger.error("Payment failed", { orderId: "456", amount: 99.99 });
// Template literals (creates searchable attributes)
Sentry.logger.info(Sentry.logger.fmt`User ${userId} purchased ${productName}`);javascript
Sentry.logger.info("User logged in", { userId: "123" });
Sentry.logger.error("Payment failed", { orderId: "456", amount: 99.99 });
// 模板字面量(可创建可搜索属性)
Sentry.logger.info(Sentry.logger.fmt`User ${userId} purchased ${productName}`);Third-party integrations
第三方集成
| Library | Integration | Min SDK |
|---|---|---|
| Pino | | 10.18.0+ |
| Winston | | 9.13.0+ |
| Consola | | 10.12.0+ |
| 库 | 集成方式 | 最低SDK版本 |
|---|---|---|
| Pino | | 10.18.0+ |
| Winston | | 9.13.0+ |
| Consola | | 10.12.0+ |
Python Setup
Python 配置步骤
1. Verify SDK version
1. 验证SDK版本
bash
pip show sentry-sdk | grep Versionbash
pip show sentry-sdk | grep Version2. Enable in init()
2. 在init()中启用
python
sentry_sdk.init(
dsn="YOUR_DSN",
enable_logs=True,
)python
sentry_sdk.init(
dsn="YOUR_DSN",
enable_logs=True,
)3. Stdlib logging capture (optional)
3. 捕获标准库日志(可选)
python
from sentry_sdk.integrations.logging import LoggingIntegration
integrations=[LoggingIntegration(sentry_logs_level=logging.WARNING)]python
from sentry_sdk.integrations.logging import LoggingIntegration
integrations=[LoggingIntegration(sentry_logs_level=logging.WARNING)]4. Use structured logging
4. 使用结构化日志
python
from sentry_sdk import logger as sentry_logger
sentry_logger.info("User logged in: {user_id}", user_id="123")
sentry_logger.error("Payment failed", order_id="456", amount=99.99)python
from sentry_sdk import logger as sentry_logger
sentry_logger.info("User logged in: {user_id}", user_id="123")
sentry_logger.error("Payment failed", order_id="456", amount=99.99)Loguru integration
Loguru 集成
python
from sentry_sdk.integrations.loguru import LoguruIntegration
integrations=[LoguruIntegration(sentry_logs_level=LoggingLevels.WARNING.value)]python
from sentry_sdk.integrations.loguru import LoguruIntegration
integrations=[LoguruIntegration(sentry_logs_level=LoggingLevels.WARNING.value)]Ruby Setup
Ruby 配置步骤
1. Verify SDK version
1. 验证SDK版本
bash
bundle show sentry-rubybash
bundle show sentry-ruby2. Enable in init
2. 在初始化中启用
ruby
Sentry.init do |config|
config.dsn = "YOUR_DSN"
config.enable_logs = true
config.enabled_patches = [:logger] # Optional: capture stdlib Logger
endruby
Sentry.init do |config|
config.dsn = "YOUR_DSN"
config.enable_logs = true
config.enabled_patches = [:logger] # 可选:捕获标准库Logger日志
end3. Use structured logging
3. 使用结构化日志
ruby
Sentry.logger.info("User logged in")
Sentry.logger.error("Payment failed. Order: %{order_id}", order_id: "456")ruby
Sentry.logger.info("User logged in")
Sentry.logger.error("Payment failed. Order: %{order_id}", order_id: "456")Log Filtering
日志过滤
JavaScript
JavaScript
javascript
beforeSendLog: (log) => log.level === "info" ? null : log,javascript
beforeSendLog: (log) => log.level === "info" ? null : log,Python
Python
python
def before_send_log(log, hint):
return None if log["severity_text"] == "info" else logpython
def before_send_log(log, hint):
return None if log["severity_text"] == "info" else logTroubleshooting
故障排除
| Issue | Solution |
|---|---|
| Logs not appearing | Verify SDK version, check |
| Too many logs | Use |
| Console not captured | Add |
| 问题 | 解决方案 |
|---|---|
| 日志未显示 | 验证SDK版本,检查 |
| 日志过多 | 使用 |
| 控制台日志未被捕获 | 将 |