baibaratsky / yii2-rollbar
为 Yii2 的 Rollbar
v1.9.1
2024-01-16 11:32 UTC
Requires
- php: >=8.0
- rollbar/rollbar: ^3.1 || ^4.0.2
- yiisoft/yii2: ^2.0
Requires (Dev)
- vimeo/psalm: ^5.1
README
此扩展是整合 Rollbar 服务到您的 Yii2 应用程序的方式。对于 Yii 1.*,请使用 yii-rollbar。
安装
安装此扩展的首选方式是通过 composer。
安装,请运行
$ php composer.phar require baibaratsky/yii2-rollbar:1.9.*
或添加
"baibaratsky/yii2-rollbar": "1.9.*"
到您的 composer.json
文件的 require
部分。
如果您想与 Yii 2.0.13 之前的版本 一起使用,则需要 1.6.*
版本的 yii2-rollbar。
使用方法
- 在您的 global
main.php
配置文件中添加组件配置
'bootstrap' => ['rollbar'], 'components' => [ 'rollbar' => [ 'class' => 'baibaratsky\yii\rollbar\Rollbar', 'accessToken' => 'POST_SERVER_ITEM_ACCESS_TOKEN', // You can specify exceptions to be ignored by yii2-rollbar: // 'ignoreExceptions' => [ // ['yii\web\HttpException', 'statusCode' => [400, 404]], // ['yii\web\HttpException', 'statusCode' => [403], 'message' => ['This action is forbidden']], // ], ], ],
- 在您的 web 配置文件中添加 web 错误处理器配置
'components' => [ 'errorHandler' => [ 'class' => 'baibaratsky\yii\rollbar\web\ErrorHandler', // You can include additional data in a payload: // 'payloadDataCallback' => function (\baibaratsky\yii\rollbar\web\ErrorHandler $errorHandler) { // return [ // 'exceptionCode' => $errorHandler->exception->getCode(), // 'rawRequestBody' => Yii::$app->request->getRawBody(), // ]; // }, ], ],
- 在您的 console 配置文件中添加 console 错误处理器配置
'components' => [ 'errorHandler' => [ 'class' => 'baibaratsky\yii\rollbar\console\ErrorHandler', ], ],
异常的负载
如果您想将一些额外的数据发送到 Rollbar,可以通过实现 WithPayload
接口来完成。
use baibaratsky\yii\rollbar\WithPayload; class SomeException extends \Exception implements WithPayload { public function rollbarPayload() { return ['foo' => 'bar']; } }
日志目标
您可能想将 Yii::error()
、Yii::info()
等生成的日志收集到 Rollbar 中。在您的配置文件中添加以下代码
'log' => [ 'targets' => [ [ 'class' => 'baibaratsky\yii\rollbar\log\Target', 'levels' => ['error', 'warning', 'info'], // Log levels you want to appear in Rollbar // It is highly recommended that you specify certain categories. // Otherwise, the exceptions and errors handled by the error handlers will be duplicated. 'categories' => ['application'], ], ], ],
日志目标还会将 category
和 request_id
参数附加到日志消息中。如果想要有类似于 yii2-debug 的分组,request_id
将非常有用。