PHP code example of sheershoff / yii2-sentry-component

1. Go to this page and download the library: Download sheershoff/yii2-sentry-component 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/ */

    

sheershoff / yii2-sentry-component example snippets


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

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

use sheershoff\sentry\Log;
// some code here
try{
    $model1->save();
}catch (\Exception $e){
    Log::warning([
        'msg' => 'ExceptionWarning', // event name that will be sent to Sentry
        'data' => [ // extra data for the event
            '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;
class SyslogJsonTarget extends \yii\log\SyslogTarget
{
	/**
	 * @inheritdoc
	 */
	public function formatMessage($message)
	{
		list($text, $level, $category, $timestamp) = $message;
		$level = \yii\log\Logger::getLevelName($level);
		if (!is_string($text)) {
			$text = \yii\helpers\Json::encode($text);
		} else {
			$text = \yii\helpers\Json::encode(['rawstring' => $text]);
		}
		$prefix = $this->getMessagePrefix($message);
		return "{$prefix}[$level][$category] $text";
	}
}