CodeIgniter Forums
[Quick Tutorial] - Integration of Sentry or Bugsnag With CI4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forumdisplay.php?fid=9)
+--- Thread: [Quick Tutorial] - Integration of Sentry or Bugsnag With CI4 (/showthread.php?tid=76315)



[Quick Tutorial] - Integration of Sentry or Bugsnag With CI4 - napa - 05-02-2020

I make this cause I can not find anything related into the forums and I have to get it working on my own, found several questions on how to do it but no one answers and theres no much information about in CI4 just the CI3.

Feel free to improve my answer, is just to help new ones.

There will be some previous steps recomended.

1.- Use composer to install CI4.
Code:
composer create-project codeigniter4/appstarter yourproject

2.- Use composer for install the Sentry SDK or Bugsnag

Code:
composer require sentry/sdk:2.1.0
composer require bugsnag/bugsnag:^3.0

3.- Check in your php.ini the following:


- tmp directory must be writtable for CURL (even in windows i use a folder into my project)
- in Windows ca_bundle installed download latest file from https://curl.haxx.se/docs/caextract.html and add to php.ini:
Code:
[curl]
curl.cainfo = "D:\php_cert\cacert.pem"
[openssl]
openssl.cafile = "D:\php_cert\cacert.pem"

4.- You are ready to go into our project files, I think the best way to integrate must be with events so let go to edit the Events.php with the following:

Sentry:
Code:
Events::on('post_controller_constructor', function () {
    \Sentry\init(['dsn' => 'YOUR_DSN' ]);
});
Bugsnag
Code:
Events::on('post_controller_constructor', function () {
    $bugsnag = \Bugsnag\Client::make('YOUR_ID');
    \Bugsnag\Handler::registerWithPrevious($bugsnag);
});

5.- Test IT!!! it just works!!!


RE: [Quick Tutorial] - Integration of Sentry or Bugsnag With CI4 - Tor2r - 06-26-2021

Thanks, worked like a charm!

Didn't understand the part about curl and php.ini, so I skipped it Wink


RE: [Quick Tutorial] - Integration of Sentry or Bugsnag With CI4 - paulmartinez - 07-21-2021

(05-02-2020, 12:31 PM)napa Wrote: I make this cause I can not find anything related into the forums and I have to get it working on my own, found several questions on how to do it but no one answers and theres no much information about in CI4 just the CI3.

Feel free to improve my answer, is just to help new ones.

There will be some previous steps recomended.

1.- Use composer to install CI4.
Code:
composer create-project codeigniter4/appstarter yourproject

2.- Use composer for install the Sentry SDK or Bugsnag

Code:
composer require sentry/sdk:2.1.0
composer require bugsnag/bugsnag:^3.0

3.- Check in your php.ini the following:


- tmp directory must be writtable for CURL (even in windows i use a folder into my project)
- in Windows ca_bundle installed download latest file from https://curl.haxx.se/docs/caextract.htmlcinema apk and add to php.ini:
Code:
[curl]
curl.cainfo = "D:\php_cert\cacert.pem"
[openssl]
openssl.cafile = "D:\php_cert\cacert.pem"

4.- You are ready to go into our project files, I think the best way to integrate must be with events so let go to edit the Events.php with the following:

Sentry:
Code:
Events::on('post_controller_constructor', function () {
\Sentry\init(['dsn' => 'YOUR_DSN' ]);
});
Bugsnag
Code:
Events::on('post_controller_constructor', function () {
$bugsnag = \Bugsnag\Client::make('YOUR_ID');
\Bugsnag\Handler::registerWithPrevious($bugsnag);
});

5.- Test IT!!! it just works!!!

It worked for me too. Thank you so much, man!


RE: [Quick Tutorial] - Integration of Sentry or Bugsnag With CI4 - zakirs - 06-11-2022

can you please make this for ci3 ?


RE: [Quick Tutorial] - Integration of Sentry or Bugsnag With CI4 - Tor2r - 08-22-2022

(06-11-2022, 03:13 AM)zakirs Wrote: can you please make this for ci3 ?

Install as above. Then in config/hooks.php add:


PHP Code:
$hook['pre_system'] = function() {
  require_once APPPATH.'/vendor/autoload.php';
  $GLOBALS['bugsnag'] = Bugsnag\Client::make("YOUR-BUGSNAG-KEY");
  Bugsnag\Handler::register($GLOBALS['bugsnag']);
}; 

For testing, run the following in a controller:
PHP Code:
$GLOBALS['bugsnag']->notifyError('ErrorType''A wild error appeared!'); 



RE: [Quick Tutorial] - Integration of Sentry or Bugsnag With CI4 - dioncodes - 09-14-2022

(05-02-2020, 12:31 PM)napa Wrote: Sentry:
Code:
Events::on('post_controller_constructor', function () {
\Sentry\init(['dsn' => 'YOUR_DSN' ]);
});
Bugsnag
Code:
Events::on('post_controller_constructor', function () {
$bugsnag = \Bugsnag\Client::make('YOUR_ID');
\Bugsnag\Handler::registerWithPrevious($bugsnag);
});

5.- Test IT!!! it just works!!!
Is there a specific reason you are using the post_controller_constructor hook instead of pre_system? I think pre_system is the first hook being called and should also handle errors that occur before a controller is loaded.
However, I have the following issue with Sentry, that all errors are reported twice (once with "Warning:" or "Error:" prefix and once without). Anyone got an idea?


RE: [Quick Tutorial] - Integration of Sentry or Bugsnag With CI4 - ToddGuy - 10-31-2022

(07-21-2021, 08:39 AM)paulmartinez Wrote:
(05-02-2020, 12:31 PM)napa Wrote: I make this cause I can not find anything related into the forums and I have to get it working on my own, found several questions on how to do it but no one answers and theres no much information about in CI4 just the CI3.

Feel free to improve my answer, is just to help new ones.

There will be some previous steps recomended.

1.- Use composer to install CI4.
Code:
composer create-project codeigniter4/appstarter yourproject

2.- Use composer for install the Sentry SDK or Bugsnag

Code:
composer require sentry/sdk:2.1.0
composer require bugsnag/bugsnag:^3.0

3.- Check in your php.ini the following:


- tmp directory must be writtable for CURL (even in windows i use a folder into my project)
- in Windows ca_bundle installed download latest file from https://curl.haxx.se/docs/caextract.htmlcinema apk and add to php.ini:
Code:
[curl]
curl.cainfo = "D:\php_cert\cacert.pem"
[openssl]
openssl.cafile = "D:\php_cert\cacert.pem"

4.- You are ready to go into our project files, I think the best way to integrate must be with events so let go to edit the Events.php with the following:

Sentry:
Code:
Events::on('post_controller_constructor', function () {
\Sentry\init(['dsn' => 'YOUR_DSN' ]);
});
Bugsnag
Code:
Events::on('post_controller_constructor', function () {
$bugsnag = \Bugsnag\Client::make('YOUR_ID');
\Bugsnag\Handler::registerWithPrevious($bugsnag);
});

5.- Test IT!!! it just works!!!

It worked for me too. Thank you so much, man!

(06-11-2022, 03:13 AM)zakirs Wrote: can you please make this for ci3 ?



RE: [Quick Tutorial] - Integration of Sentry or Bugsnag With CI4 - IvanRog - 08-16-2023

(06-11-2022, 03:13 AM)zakirs Wrote: can you please make this for ci3 ?
composer require sentry/sdk

Codeigniter 3:

application/config/config.php  -

PHP Code:
$config['enable_hooks'] = TRUE
application/config/hooks.php -


PHP Code:
$hook['post_controller_constructor'] = function() {
    \Sentry\init(['dsn' => 'YOUR_DSN']);
}; 


example:

   
PHP Code:
    try {
          // run();
        } catch (\Throwable $exception) {
            \Sentry\captureException($exception);
        


Codeigniter 4:

aap/Config/Events.php


PHP Code:
Events::on('post_controller_constructor', function () {
\
Sentry\init(['dsn' => 'YOUR_DSN' ]);
}); 



example:

       
PHP Code:
try {
          // run();
        } catch (\Throwable $exception) {
            \Sentry\captureException($exception);