fl0v/yii2-rollbar

There is no license information available for the latest version (1.0.1) of this package.

Rollbar for Yii2

Installs: 10 068

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 1

Open Issues: 0

Type:yii2-extension

1.0.1 2019-03-12 10:20 UTC

This package is auto-updated.

Last update: 2024-12-13 00:12:17 UTC


README

This extension is a fork from baibaratsky/yii2-rollbar and eroteev/yii2-rollbar. For Yii 1.x use baibaratsky/yii-rollbar.

Installation

The preferred way to install this extension is through composer.

To install, either run

$ php composer.phar require fl0v/yii2-rollbar

or add

"fl0v/yii2-rollbar": "*"

to the require section of your composer.json file.

Usage

  • Add the component configuration in your global config file:
'components' => [
    'rollbar' => [
        'class' => 'fl0v\yii2\rollbar\RollbarLoader',
        'config' => [
            'environment' => '{your_environment}',
            'access_token' => '{rollber_access_token}',
            // other Rollbar config settings
        ],
    ],
]
  • Add the web error handler configuration in your web config file:
'components' => [
    'errorHandler' => [
        'class' => 'fl0v\yii2\rollbar\handlers\WebErrorHandler',
    ],
],
  • Add the console error handler configuration in your console config file:
'components' => [
    'errorHandler' => [
        'class' => 'fl0v\yii2\rollbar\handlers\ConsoleErrorHandler',
    ],
],

Payload from your exceptions

If you want your exceptions to send some additional data to Rollbar, it is possible by implementing PayloadInterface.

use fl0v\yii2\rollbar\PayloadInterface;
 
class SomeException extends \Exception implements PayloadInterface
{
    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:

'components' => [
    'log' => [
        'targets' => [
            [
                'class' => 'fl0v\yii2\rollbar\RollbarTarget',
                'levels' => ['error', 'warning', 'info'], // Log levels you want to appear in Rollbar             
                'categories' => ['application'],
            ],
        ],
    ],
],

Rollbar Javascript

Rollbar offers Javascript debugger aswell, see https://docs.rollbar.com/docs/javascript. To use it in Yii2 there is fl0v\yii2\rollbar\RollbarAsset that you can register in your main template.

RollbarAsset is used independently of the server side component, to configure it use assetManager. For the config part of RollbarAsset checkout Rollbar reference https://docs.rollbar.com/docs/rollbarjs-configuration-reference#section-reference.

'assetManager' => [
    ...
    'bundles' => [
        ....
        'fl0v\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,
                    ],
                    'custom' => [
                        'myData' => 'asd',
                        'key' => uniqid(),
                    ],
                ];
            ],
        ],
    ],
],