Add Sentry to PHP
Requirements
- A sentry account.
- Senrty DSN
Install
Add the sentry sdk to your project.
composer require sentry/sdk
Configure
To capture all errors, even the one during the startup of your application, you should initialize the Sentry PHP SDK as soon as possible.
Add a value
sentry_dsn
inconfig/params.php
.<?php
return [
'bsVersion' => 4,
'app_name' => 'UIMS',
'sentry_dsn' => '<YOUR SENRY DSN>'
];Create a file
components/SentryComponent.php
.<?php
namespace app\components;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
class SentryComponent extends Component {
public function __construct($config=[]) {
parent::__construct($config);
\Sentry\init(['dsn' => Yii::$app->params['sentry_dsn'] ]);
}
}In the
config/web.php
add sentry tobootstrap
.'bootstrap' => ['log', 'queue', 'sentry'],
Initialize sentry by adding it in the
components
.'sentry' => [
'class' => 'app\components\SentryComponent',
],
Usage
To check if sentry is integrated properly thow an exception from anywhere in the application.
throw new \Exception("My first Sentry error!");
Go to the path where the error is added. If everything went as planned the error should be logged in the sentry dashboard.
NOTE: Do not forget to remove this error after.