elzix / yii2-sentry
为 Sentry(SDK v3)提供的 Yii2 集成
Requires
- php: ^8.1
- sentry/sentry: ^3.18
- yiisoft/yii2: ~2.0.45
Requires (Dev)
- php-http/mock-client: ^1.0
Suggests
- http-interop/http-factory-guzzle: Recommended HTTP factory for Sentry SDK
- php-http/curl-client: Recommended HTTP client for Sentry SDK
This package is auto-updated.
Last update: 2024-09-14 20:25:26 UTC
README
使用 Sentry PHP SDK v3 为 Sentry 提供的 Yii2 集成。
这是 Olegtsvetkov 的 Yii2 Sentry v2 包的一个副本,只有少量修改。
安装
安装此包的首选方式是通过 composer
composer require elzix/yii2-sentry:^1.0
该包默认不提供任何 HTTP 传输。要使用推荐的 HTTP 客户端安装包,请使用特殊的元包
composer require elzix/yii2-sentry-curl-client:^1.0
基本用法
将 "sentry" 组件添加到应用程序的配置和引导中,然后按以下方式配置日志目标
<?php return [ 'id' => 'my-app', 'bootstrap' => [ 'sentry', 'log', ], 'components' => [ 'sentry' => [ 'class' => elzix\Yii2\Sentry\Component::class, 'dsn' => 'https://abcdefghijklmnopqrstuvwxyz123456@sentry.io/0000000', ], 'log' => [ 'targets' => [ [ 'class' => elzix\Yii2\Sentry\LogTarget::class, 'levels' => ['error', 'warning'], 'except' => [ 'yii\web\HttpException:40*', ], ], ], ], ], ];
别忘了将 DSN 更改为自己的。
在此之后,所有异常(除了黑名单中的),PHP 错误和对 Yii::error()
和 Yii:warning()
的调用都将记录到 Sentry。
强烈建议将所有 Yii 的异常列入黑名单以处理 40x 响应,因为它们用于处理请求,并不表示任何类型的问题。
组件默认提供了有关请求的详细信息,例如
- 请求方法
- 请求 URL(包括查询字符串)
- 请求头
- 请求体
- 解析的路由(用于标签)
- 登录用户 ID
- 访客的 IP 地址
- 访客的 Cookie(启用发送默认 PII)
- 应用环境(从
YII_ENV
获取) - 异常的堆栈跟踪
关于 LogTarget 的重要细节
捆绑在 Log Target 中的将只向 Sentry 发送 一个 消息。此消息将基于日志条目中最高严重级别的条目。所有其他条目都将放入消息的额外字段 "logs" 中
高级用法
Sentry 客户端配置
组件为 Sentry 客户端提供了开箱即用的配置。可以使用 Component::$sentrySettings
属性进行覆盖和扩展。直接使用 Sentry PHP SDK 的选项。
此外,Sentry 的 ClientBuilder 是使用 Yii 的容器创建的,这允许自定义构建器的注入。
个人识别信息(PII)处理
默认情况下,Sentry 在其一边提供 PII 处理,但它不提供对 PII 剥离过程的完全控制。因此,Yii2 Sentry 包能够从请求头和请求体中剥离 PPI。
组件配置示例,包含 PII 相关设置的全列表
<?php [ 'class' => elzix\Yii2\Sentry\Component::class, 'dsn' => 'https://abcdefghijklmnopqrstuvwxyz123456:abcdefghijklmnopqrstuvwxyz123456@sentry.io/0000000', 'integrations' => [ [ 'class' => elzix\Yii2\Sentry\Integration::class, // Headers that should not be send to Sentry at all 'stripHeaders' => ['cookie', 'set-cookie'], // Headers which values should be filtered before sending to Sentry 'piiHeaders' => ['custom-token-header', 'authorization'], // Body fields which values should be filtered before sending to Sentry 'piiBodyFields' => [ 'controller/action' => [ 'field_1' => [ 'field_2', ], 'field_2', ], 'account/login' => [ 'email', 'password', ], ], // Text to replace PII values with 'piiReplaceText' => '[Filtered PII]', ], Sentry\Integration\ErrorListenerIntegration::class, ], ]