evolcon / yii2-sentry
此包最新版本(2.0.0)没有可用的许可信息。
Yii2 sentry
2.0.0
2021-03-24 22:55 UTC
Requires
- php: >=8.0.2
- sentry/sdk: 3.1.*
README
安装
composer require evolcon/yii2-sentry:@dev
将 SentryComponent 添加到应用配置
'components' => [
'sentry' => [
'class' => evolcon\sentry\SentryComponent::class,
'dsn' => 'https://fsdhbk67bhkfa424eehb678agj66b7@sentry.io/2588150',
],
],
要禁用通知,将 enabled 属性设置为 false
'components' => [
'sentry' => [
'class' => evolcon\sentry\SentryComponent::class,
'dsn' => 'https://fsdhbk67bhkfa424eehb678agj66b7@sentry.io/2588150',
'enabled' => false,
],
],
为组件 'log' 添加类 SentryTarget 属性 'targets'
'components' => [
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'flushInterval' => 1,
'targets' => [
[
'class' => evolcon\sentry\SentryTarget::class,
'exportInterval' => 1,
'levels' => ['error', 'warning'],
'except' => [
'yii\web\HttpException:429', // TooManyRequestsHttpException
'yii\web\HttpException:401', // UnauthorizedHttpException
],
'userData' => ['id', 'email', 'role'],
],
]
],
],
## 用户数据收集
将 userData 属性添加到 SentryTarget,并列出需要收集的属性数组
'components' => [
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'flushInterval' => 1,
'targets' => [
[
'class' => evolcon\sentry\SentryTarget::class,
'exportInterval' => 1,
'levels' => ['error', 'warning'],
'except' => [
'yii\web\HttpException:429', // TooManyRequestsHttpException
'yii\web\HttpException:401', // UnauthorizedHttpException
],
'userData' => ['id', 'email', 'role'],
],
]
],
],
默认情况下,sentry 目标使用应用程序的 'user' 组件。如果您需要覆盖组件,请配置属性 userComponent
注意:组件必须是 \yii\web\User 类的实例
'components' => [
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'flushInterval' => 1,
'targets' => [
[
'class' => evolcon\sentry\SentryTarget::class,
'userComponent' => 'user', //the component name
'userData' => ['id', 'email', 'role'],
],
]
],
],
有时我们需要分离日志,例如,将 warning 和 error 分离,并将它们发送到不同的 sentry 项目。为此,我们需要覆盖 SentryTarget 的属性 sentryComponent,并设置所需的组件名称
'components' => [
'sentryWarnings' => [
'class' => evolcon\sentry\SentryComponent::class,
'dsn' => 'https://fsdhbk67bhkfa424eehb678agj66b7@sentry.io/55555555',
],
'sentryErrors' => [
'class' => evolcon\sentry\SentryComponent::class,
'dsn' => 'https://fsdhbk67bhkfa424eehb678agj66b7@sentry.io/999999999',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'flushInterval' => 1,
'targets' => [
[
'class' => evolcon\sentry\SentryTarget::class,
'sentryComponent' => 'sentryWarnings',
'levels' => ['warning'], //only warnings
],
[
'class' => evolcon\sentry\SentryTarget::class,
'sentryComponent' => 'sentryErrors',
'levels' => ['error'], //only errors
],
]
],
],
带标签的异常
use evolcon\sentry\SilentException;
throw (new SilentException('Error message'))
->addTag('tagName', 'tagValue')
->addTag('tagName2', 'tagValue')
->addExtra('extraName', 'extraValue')
->addExtra('extraName2', 'extraValue');
多个标签和额外信息
throw (new SilentException('Error message'))
->addTags(['tagName' => 'tagValue', 'tagName2' => 'tagValue'])
->addExtras(['extraName' => 'extraValue', 'extraName2' => 'extraValue']);
在某些情况下,当需要调试代码并保持项目容错性时,可能需要不抛出异常。在这种情况下,您可以创建 SilentException 类的实例并调用方法 save()
try {
// my code
} catch(\Throwable $e) {
(new SilentException($e->getMessage()))
->addTags(['tagName' => 'tagValue', 'tagName2' => 'tagValue'])
->addExtras(['extraName' => 'extraValue', 'extraName2' => 'extraValue'])
->save(__METHOD__);
}