sorokinmedia/yii2-rollbar

Rollbar for Yii2

dev-master 2020-01-04 14:09 UTC

This package is auto-updated.

Last update: 2024-04-04 23:07:27 UTC


README

Packagist Packagist

This extension is the way to integrate Rollbar service with your Yii2 application.

Installation

The preferred way to install this extension is through composer.

To install, either run

$ php composer.phar require sorokinmedia/yii2-rollbar

or add

"sorokinmedia/yii2-rollbar": "dev-master"

to the require section of your composer.json file.

If you want to use it with Yii prior to 2.0.13, you need yii2-rollbar of the version 1.6.*.

Usage

  1. Add the component configuration in your global main.php config file:
'bootstrap' => ['rollbar'],
'components' => [
    'rollbar' => [
        'class' => \sorokinmedia\rollbar\Rollbar::class,
        'accessToken' => 'YOUR_ACCESS_TOKEN',
        'environment' => 'local',
        'root' => '@root',
        // You can specify exceptions to be ignored by yii2-rollbar:
        'ignoreExceptions' => [
            ['yii\web\HttpException', 'statusCode' => [400, 403, 404]],
            ['yii\web\ForbiddenHttpException', 'statusCode' => [403]],
            ['yii\web\UnauthorizedHttpException', 'statusCode' => [401,403]],
        ],
    ],
],
  1. Add the web error handler configuration in your web config file:
'components' => [
    'errorAction' => 'site/error',
    'class' => \sorokinmedia\rollbar\web\ErrorHandler::class,

    // You can include additional data in a payload:
    'payloadDataCallback' => function (\sorokinmedia\rollbar\web\ErrorHandler $errorHandler) {
        return [
            'exceptionCode' => $errorHandler->exception->getCode(),
            'rawRequestBody' => Yii::$app->request->getRawBody(),
        ];
    },
],
  1. Add the console error handler configuration in your console config file:
'components' => [
    'errorHandler' => [
        'class' => \sorokinmedia\rollbar\console\ErrorHandler::class,
    ],
],

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' => \sorokinmedia\rollbar\log\Target::class,
           '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.