Skip to main content

Add Sentry to PHP

Requirements

  1. A sentry account.
  2. 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.

  1. Add a value sentry_dsn in config/params.php.

    <?php
    return [
    'bsVersion' => 4,
    'app_name' => 'UIMS',
    'sentry_dsn' => '<YOUR SENRY DSN>'
    ];
  2. 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'] ]);
    }
    }
  3. In the config/web.php add sentry to bootstrap.

    '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.