简体   繁体   中英

I can't add sentry to php slim

I need to add php sentry error handler to my slim 3 project. how can I do so ? where should put sentry integration code? what I'm doing now is :

// monolog
$container['logger'] = function ($c) {
    $settings = $c->get('settings')['logger'];
    $logger = new Monolog\Logger($settings['name']);
    $logger->pushProcessor(new Monolog\Processor\UidProcessor());
    $logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], $settings['level']));

    $client = new Raven_Client(
        'http://key@ip:9000/2'
    );

    $handler = new Monolog\Handler\RavenHandler($client);
    $handler->setFormatter(new Monolog\Formatter\LineFormatter("%message% %context% %extra%\n"));

    $logger->pushHandler($handler);

    return $logger;
};

but I'm not getting all errors in my sentry dashboard. for example accessing undefined array indexes. thanks.

I think the best way is to just do the following (I did not test this or have ever used Slim but looking at the Slim docs this is a way to do it):

In your index.php (which should be the app entrypoint) just after require '../../vendor/autoload.php'; (the composer autoload).

Add the Raven initialization code:

$sentry = new Raven_Client('http://key@ip:9000/2');
$sentry->install();

This will configure the SDK to handle (and send) all errors, no need for the Monolog handler anymore.

If you want to integration it in a ErrorHandler class you created looking at this skeleton project might give you some ideas.

I am using a custom error handler to catch exceptions. This way i can use the default slim error handler and Sentry error reporting at the same time.

This is my code:

// initalize sentry
Sentry\init(['dsn' => 'your_dsn' ]);

// Run app
$app = (new App())->get();

// register custom error handler
$c = $app->getContainer();
$c['errorHandler'] = function ($c) {
    return function ($request, $response, $exception) use ($c) {
        // send error to sentry
        Sentry\captureException($exception);

        // invoke default error handler
        $handler = new Slim\Handlers\Error();
        return $handler->__invoke($request, $response, $exception);
    };
};

$app->run();

Not sure if this is the "recommended" way, but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM