PHP code example of laxity7 / yii2-sentry

1. Go to this page and download the library: Download laxity7/yii2-sentry library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laxity7 / yii2-sentry example snippets


'bootstrap'  => ['log', 'raven'],
'components' => [
    'raven' => [
        'class' => 'laxity7\sentry\ErrorHandler',
        'dsn'   => '', // Sentry DSN
    ],
    'log'   => [
        'targets' => [
            [
                'class'  => 'laxity7\sentry\Target',
                'levels' => ['error', 'warning'],
                'dsn'    => '', // Sentry DSN
            ],
        ],
    ],
],

Yii::warning([
    // event name that will be sent to Sentry
    'msg'  => 'SomeWarning',
    // extra data for the event
    'data' => [
        'userId'                     => Yii::$app->user->id,
        'someDataOnWarningSituation' => $controller->state,
        'modelThatCausedFailure'     => $model->attributes,
    ],
], 'eventCategory');

use laxity7\sentry\Log;
// some code here
try {
    $model->save();
} catch (\Exception $e) {
    Log::warning([
        // event name that will be sent to Sentry
        'msg'  => 'ExceptionWarning', 
        // extra data for the event
        'data' => [ 
            'userId'                     => Yii::$app->user->id,
            'someDataOnWarningSituation' => $controller->state,
            'modelThatCausedFailure'     => $model->attributes,
        ],
    ], 'exceptionCategory', $e);
}

SentryHelper::extraData($task->attributes);
throw new Exception('unknown task type');

try {
    throw new Exception('FAIL');
} catch (Exception $e) {
    SentryHelper::captureWithMessage('Fail to save model', $e);
}

namespace common\components;
use Yii;
use yii\log\Logger;
use yii\helpers\Json;
class SyslogJsonTarget extends \yii\log\SyslogTarget
{
    /**
     * @inheritdoc
     */
    public function formatMessage($message)
    {
        list($text, $level, $category, $timestamp) = $message;
        $level = Logger::getLevelName($level);
        if (!is_string($text)) {
            $text = Json::encode($text);
        } else {
            $text = Json::encode(['rawstring' => $text]);
        }
        $prefix = $this->getMessagePrefix($message);

        return "{$prefix}[$level][$category] $text";
    }
}

php composer.phar