oaksoftwaredev / yii2-rollbar
此包的最新版本(2.0.1)没有提供许可信息。
Yii2的Rollbar
2.0.1
2022-09-21 10:09 UTC
Requires
- php: >=7.1
- rollbar/rollbar: ^2
- yiisoft/yii2: ~2.0.13
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.0
- yiisoft/yii2-apidoc: ~2.1.0
README
此扩展是从baibaratsky/yii2-rollbar和eroteev/yii2-rollbar分叉而来。对于Yii 1.x使用baibaratsky/yii-rollbar。
2022-09-20 分叉以更新rollbar版本。
安装
安装此扩展的首选方式是通过composer。
要安装,运行以下命令之一:
$ php composer.phar require oaksoftwaredev/yii2-rollbar
或
"oaksoftwaredev/yii2-rollbar": "*"
添加到您的composer.json
文件的require
部分。
使用方法
- 在您的全局配置文件中添加组件配置
'components' => [ 'rollbar' => [ 'class' => 'oaksoftwaredev\yii2\rollbar\RollbarLoader', 'config' => [ 'environment' => '{your_environment}', 'access_token' => '{rollber_access_token}', 'send_message_trace' => true, 'include_exception_code_context' => true, 'include_error_code_context' => true, 'included_errno' => E_ALL, 'enabled' => 'true', 'check_ignore' => function($isUncaught, $toLog, $payload) { return \oaksoftwaredev\yii2\rollbar\helpers\IgnoreExceptionHelper::checkIgnore ($toLog, [ ['yii\web\HttpException', 'statusCode' => [400, 404]], // check properties ]); }, ], ], ]
- 在您的web配置文件中添加web错误处理器配置
'components' => [ 'errorHandler' => [ 'class' => 'oaksoftwaredev\yii2\rollbar\handlers\WebErrorHandler', ], ],
- 在您的console配置文件中添加console错误处理器配置
'components' => [ 'errorHandler' => [ 'class' => 'oaksoftwaredev\yii2\rollbar\handlers\ConsoleErrorHandler', ], ],
异常的负载
如果您想将一些额外的数据发送到Rollbar,可以通过实现PayloadInterface
来实现。
use oaksoftwaredev\yii2\rollbar\PayloadInterface; class SomeException extends \Exception implements PayloadInterface { public function rollbarPayload() { return ['foo' => 'bar']; } }
日志目标
您可能希望将Yii::error()
、Yii::info()
等生成的日志收集到Rollbar中。
在您的配置中添加以下代码
'components' => [ 'log' => [ 'targets' => [ [ 'class' => 'oaksoftwaredev\yii2\rollbar\RollbarTarget', 'levels' => ['error', 'warning', 'info'], // Log levels you want to appear in Rollbar 'categories' => ['application'], ], ], ], ],
Rollbar JavaScript
Rollbar还提供JavaScript调试器,请参阅https://docs.rollbar.com/docs/javascript。在Yii2中使用它,有一个oaksoftwaredev\yii2\rollbar\RollbarAsset
,您可以在主模板中注册它。
RollbarAsset独立于服务器端组件使用,配置它使用assetManager。有关RollbarAsset的配置部分,请参阅Rollbar参考https://docs.rollbar.com/docs/rollbarjs-configuration-reference#section-reference。
'assetManager' => [ 'bundles' => [ 'oaksoftwaredev\yii2\rollbar\RollbarAsset' => [ // Rollbar configuration 'config' => [ 'accessToken' => '{token}', 'payload' => [ 'environment' => '{environment}', ], ], // metrics to add to payload, called when the asset is registered 'payload' => function () { return [ 'person' => [ 'id' => \Yii::$app->has('user') ? (string) \Yii::$app->user->id : null, 'username' => \Yii::$app->has('user') && ! \Yii::$app->user->isGuest ? \Yii::$app->user->identity->username : null, ], ]; }, ], ], ],