Enrich your logging on Codeigniter 4 with Sentry

Davod
Jul 14, 2022

--

If you are a developer probably you know about Sentry. But as far as there isn’t any document about implementation on Codeigniter 4 here we go:

  1. Installing sentry SDK through composer:
composer require sentry/sdk

2. Add a new event with this code at the end of the file /app/Config/Events.php:

Events::on('post_controller_constructor', function () {
\Sentry\init([
'dsn' => 'https://3a19161981ca4b3e97216be79b6f5f3b@sentry.dgtteam.com/4' ,
'environment' => ENVIRONMENT,
'traces_sample_rate' => (ENVIRONMENT === 'development') ? 1.0 : 0.2,
'release' => 'ci@1.0.0',
'server_name'=>$_SERVER['SERVER_NAME']
]);
\Sentry\captureLastError();
});

post_controller_contructor is a type called immediately after your controller is instantiated.

You can add other configuration attributes to your code.

Note that you can also capture or flag in your code, even after setting the event.

try {
$this->functionFailsForSure();
} catch (\Throwable $exception) {
\Sentry\captureException($exception);
}

--

--

Davod
Davod

No responses yet