Rollbar for Yii2
This extension is the way to integrate Rollbar service with your Yii2 application. For Yii 1.* use yii-rollbar.
Installation
The preferred way to install this extension is through composer.
To install, either run
$ php composer.phar require baibaratsky/yii2-rollbar:1.5.*
or add
"baibaratsky/yii2-rollbar": "1.5.*"
to the require
section of your composer.json
file.
Usage
- Add the component configuration in your global
main.php
config file:
'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']],
// ],
],
],
- Add the web error handler configuration in your web config file:
'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(),
// ];
// },
],
],
- Add the console error handler configuration in your console config file:
'components' => [
'errorHandler' => [
'class' => 'baibaratsky\yii\rollbar\console\ErrorHandler',
],
],
Payload from your exceptions
If you want your exceptions to send some additional data to Rollbar,
it is possible by implementing the WithPayload
interface.
use baibaratsky\yii\rollbar\WithPayload;
class SomeException extends \Exception implements WithPayload
{
public function rollbarPayload()
{
return ['foo' => 'bar'];
}
}
Log Target
You may want to collect your logs produced by Yii::error()
, Yii::info()
, etc. in Rollbar.
Put the following code in your config:
'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'],
],
],
],
The log target also appends category
and request_id
parameters to the log messages.
request_id
is useful if you want to have a yii2-debug-like grouping.