PHP code example of avto-dev / sentry-laravel

1. Go to this page and download the library: Download avto-dev/sentry-laravel 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/ */

    

avto-dev / sentry-laravel example snippets




namespace App\Exceptions;

class Handler extends \Illuminate\Foundation\Exceptions\Handler
{
    // ...

    /**
     * Report or log an exception.
     *
     * @param \Exception $exception
     *
     * @return void
     */
    public function report(\Exception $exception): void
    {
        if ($this->container->bound('sentry') && $this->shouldReport($exception)) {
            try {
                $this->container->make('sentry')->captureException($exception);
            } catch (\Exception $e) {
                $this->container->make(\Psr\Log\LoggerInterface::class)->error(
                    'Cannot capture exception with sentry: ' . $e->getMessage(), ['exception' => $e]
                );
            }
        }

        parent::report($exception);
    }

    // ...
}



return [
    'channels' => [

        // ...

        'sentry' => [
            'driver' => 'sentry',
        ],
    ],
];



return [
    'channels' => [

        'stack' => [
            'driver'   => 'stack',
            'channels' => ['single', 'sentry'], // Add the Sentry log channel to the stack
        ],

        // ...
    ],
];



return [
    'channels' => [

        // ...

        'sentry' => [
            'driver' => 'sentry',
            'level'  => null, // The minimum monolog logging level at which this handler will be triggered
                              // For example: `\Monolog\Logger::ERROR`
            'bubble' => true, // Whether the messages that are handled can bubble up the stack or not
        ],
    ],
];



return [
    'channels' => [

        // ...

        'my_stacked_channel' => [
            'driver'   => 'stack',
            'channels' => ['single', 'sentry'],
            'name'     => 'my-channel'
        ],
    ],
];



\Illuminate\Support\Facades\Log::channel('my_stacked_channel')->error('My error');
bash
> $ test -f ./config/sentry.php && mv ./config/sentry.php ./config/sentry.php.old
> 
bash
$ php artisan sentry:test
[sentry] Client DSN discovered!
[sentry] Generating test event
[sentry] Sending test event
[sentry] Event sent: e6442bd7806444fc8b2710abce3599ac