sentry-setup-logging

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Setup 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
    Sentry.logger
    or
    sentry_sdk.logger
  • 用户要求“配置Sentry日志记录”或“在Sentry中捕获日志”
  • 用户希望将日志库(Pino、Winston、Loguru)与Sentry集成
  • 用户询问
    Sentry.logger
    sentry_sdk.logger
    相关内容

Quick Reference

快速参考

PlatformMin SDKEnable FlagLogger API
JavaScript9.41.0+
enableLogs: true
Sentry.logger.*
Python2.35.0+
enable_logs=True
sentry_sdk.logger.*
Ruby5.24.0+
config.enable_logs = true
Sentry.logger.*
平台最低SDK版本启用标识日志器API
JavaScript9.41.0+
enableLogs: true
Sentry.logger.*
Python2.35.0+
enable_logs=True
sentry_sdk.logger.*
Ruby5.24.0+
config.enable_logs = true
Sentry.logger.*

JavaScript Setup

JavaScript 配置步骤

1. Verify SDK version

1. 验证SDK版本

bash
grep -E '"@sentry/(nextjs|react|node|browser)"' package.json
bash
grep -E '"@sentry/(nextjs|react|node|browser)"' package.json

2. 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

第三方集成

LibraryIntegrationMin SDK
Pino
Sentry.pinoIntegration()
10.18.0+
Winston
Sentry.createSentryWinstonTransport()
9.13.0+
Consola
Sentry.createConsolaReporter()
10.12.0+
集成方式最低SDK版本
Pino
Sentry.pinoIntegration()
10.18.0+
Winston
Sentry.createSentryWinstonTransport()
9.13.0+
Consola
Sentry.createConsolaReporter()
10.12.0+

Python Setup

Python 配置步骤

1. Verify SDK version

1. 验证SDK版本

bash
pip show sentry-sdk | grep Version
bash
pip show sentry-sdk | grep Version

2. 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-ruby
bash
bundle show sentry-ruby

2. 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
end
ruby
Sentry.init do |config|
  config.dsn = "YOUR_DSN"
  config.enable_logs = true
  config.enabled_patches = [:logger]  # 可选:捕获标准库Logger日志
end

3. 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 log
python
def before_send_log(log, hint):
    return None if log["severity_text"] == "info" else log

Troubleshooting

故障排除

IssueSolution
Logs not appearingVerify SDK version, check
enableLogs
/
enable_logs
is set
Too many logsUse
beforeSendLog
to filter, reduce captured levels
Console not capturedAdd
consoleLoggingIntegration
to integrations array
问题解决方案
日志未显示验证SDK版本,检查
enableLogs
/
enable_logs
是否已设置
日志过多使用
beforeSendLog
进行过滤,减少捕获的日志级别
控制台日志未被捕获
consoleLoggingIntegration
添加到集成数组中