The preferred way to use the Sentry PHP SDK is through the Static API (global functions). This approach handles the initialization of global exception/error handlers automatically. Use ext{Sentry} extbackslash ext{init}() to set up the SDK and ext{Sentry} extbackslash ext{configureScope}() to manage contextual data like tags, user information, and extra data.
\Sentry\init(['dsn' => '___PUBLIC_DSN___' ]);
\Sentry\configureScope(function (\Sentry\State\Scope $scope): void {
$scope->setTag('page_locale', 'de-at');
$scope->setUser(['email' => 'john.doe@example.com']);
$scope->setLevel(\Sentry\Severity::warning());
$scope->setExtra('character_name', 'Mighty Fighter');
});
// The following capture call will contain the data from the previous configured Scope
try {
thisFunctionThrows(); // -> throw new \Exception('foo bar');
} catch (\Exception $exception) {
\Sentry\captureException($exception);
}
\Sentry\addBreadcrumb(new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting', 'Message'));