nohnaimer/

yii2-sentry

Yii2 的 Sentry 日志器

安装次数: 15,713

依赖者: 0

推荐者: 0

安全性: 0

星标: 0

关注者: 0

分支: 50

类型:yii2-extension

2.0.0 2024-02-09 10:01 UTC

README

Latest Stable Version Total Downloads License

安装

composer require nohnaimer/yii2-sentry

在应用配置中添加目标类

return [
    'components' => [
        'sentry' => [
            'class' => 'nohnaimer\sentry\Component',
            'dsn' => 'http://2682ybvhbs347:235vvgy465346@sentry.io/1',
            // Additional options for `Sentry\init`:
            'clientOptions' => [
                'release' => 'my-project-name@2.3.12',
                //Performance Monitoring
                'traces_sample_rate' => 1.0,
            ],
            //collect JavaScript errors, default false
            'jsNotifier' => true,
            //Collect javascript errors to different project
            'jsDsn' => 'http://6cfe124dfafd4fa98ac0a7f7cfccf187@sentry.io/2',
            // Additional options for javascript `Sentry\init`:
            'jsClientOptions' => [
                'release' => 'my-project-name@2.3.12',
                //Performance Monitoring
                'integrations' => '[new Sentry.Integrations.BrowserTracing()]',
                'tracesSampleRate' => 0.2,
            ],
            // Write the context information (the default is true):
            'context' => true,
            //add Environment application
            'environment' => 'test',
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'nohnaimer\sentry\Target',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
    ],
];

用法

编写简单消息

\Yii::error('message', 'category');

带有额外数据的消息

\Yii::warning([
    'msg' => 'message',
    'extra' => 'value',
], 'category');

额外回调

extraCallback 属性可以作为可调用函数修改额外数据

    'targets' => [
        [
            'class' => 'nohnaimer\sentry\Target',
            'levels' => ['error', 'warning'],
            'extraCallback' => function ($message, $extra) {
                // some manipulation with data
                $extra['some_data'] = \Yii::$app->someComponent->someMethod();
                return $extra;
            }
        ],
    ],

标签

带有附加标签的消息。如果需要为事件添加附加标签,请将 tags 键添加到消息中。标签是各种键/值对,用于分配给事件,并可以用于后续的细分或快速查找相关事件。

示例

\Yii::warning([
    'msg' => 'message',
    'extra' => 'value',
    'tags' => [
        'extraTagKey' => 'extraTagValue',
    ]
], 'category');

有关标签的更多信息,请参阅 https://docs.sentry.io/learn/context/#tagging-events

附加上下文

您可以在日志之前调用 \Sentry\configureScope() 来添加附加上下文(例如用户信息、指纹等)。例如,在主配置中的 beforeAction 事件上(实际位置取决于您的项目)

return [
    // ...
    'on beforeAction' => function (\yii\base\ActionEvent $event) {
        /** @var \yii\web\User $user */
        $user = Yii::$app->has('user', true) ? Yii::$app->get('user', false) : null;
        if ($user && ($identity = $user->getIdentity(false))) {
            \Sentry\configureScope(function (\Sentry\State\Scope $scope) use ($identity) {
                $scope->setUser([
                    // User ID and IP will be added by logger automatically
                    'username' => $identity->username,
                    'email' => $identity->email,
                ]);
            });
        }
    
        return $event->isValid;
    },
    // ...
];

日志级别

Yii2 日志级别转换为 Sentry 级别

\yii\log\Logger::LEVEL_ERROR => 'error',
\yii\log\Logger::LEVEL_WARNING => 'warning',
\yii\log\Logger::LEVEL_INFO => 'info',
\yii\log\Logger::LEVEL_TRACE => 'debug',
\yii\log\Logger::LEVEL_PROFILE_BEGIN => 'debug',
\yii\log\Logger::LEVEL_PROFILE_END => 'debug',