PHP code example of cakephp-biztech / cake-sentry
1. Go to this page and download the library: Download cakephp-biztech/cake-sentry 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/ */
cakephp-biztech / cake-sentry example snippets
// in `config/app.php`
return [
'Sentry' => [
'dsn' => YOUR_SENTRY_DSN_HERE
]
];
public function bootstrap()
{
parent::bootstrap();
$this->addPlugin(\Biztech\CakeSentry\Plugin::class);
}
// in `config/app.php`
'Error' => [
'skipLog' => [
NotFoundException::class,
MissingRouteException::class,
MissingControllerException::class,
],
]
use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
class SentryOptionsContext implements EventListenerInterface
{
public function implementedEvents()
{
return [
'CakeSentry.Client.afterSetup' => 'setServerContext',
];
}
public function setServerContext(Event $event)
{
/** @var Client $subject */
$subject = $event->getSubject();
$options = $subject->getHub()->getClient()->getOptions();
$options->setEnvironment('test_app');
$options->setRelease('2.0.0@dev');
}
}
EventManager::instance()->on(new SentryOptionsContext());
use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
use Cake\Http\ServerRequest;
use Cake\Http\ServerRequestFactory;
use Sentry\State\Scope;
use function Sentry\configureScope as sentryConfigureScope;
class SentryErrorContext implements EventListenerInterface
{
public function implementedEvents()
{
return [
'CakeSentry.Client.beforeCapture' => 'setContext',
];
}
public function setContext(Event $event)
{
if (PHP_SAPI !== 'cli') {
/** @var ServerRequest $request */
$request = $event->getData('request') ?? ServerRequestFactory::fromGlobals();
$request->trustProxy = true;
sentryConfigureScope(function (Scope $scope) use ($request, $event) {
$scope->setTag('app_version', $request->getHeaderLine('App-Version') ?: 1.0);
$exception = $event->getData('exception');
if ($exception) {
assert($exception instanceof \Exception);
$scope->setTag('status', $exception->getCode());
}
$scope->setUser(['ip_address' => $request->clientIp()]);
$scope->setExtras([
'foo' => 'bar',
'request attributes' => $request->getAttributes(),
]);
});
}
}
}
EventManager::instance()->on(new SentryErrorContext());
class SentryErrorContext implements EventListenerInterface
{
public function implementedEvents()
{
return [
'CakeSentry.Client.afterCapture' => 'callbackAfterCapture',
];
}
public function callbackAfterCapture(Event $event)
{
$lastEventId = $event->getData('lastEventId');
}
}