Bugsnag plugin for Craft CMS
Log Craft errors/exceptions to Bugsnag., (*1)
Installation
You can install Bugsnag via the plugin store, or through Composer., (*2)
Craft Plugin Store
To install Bugsnag, navigate to the Plugin Store section of your Craft control panel, search for Bugsnag, and click the Try button., (*3)
Composer
You can also add the package to your project using Composer., (*4)
-
Open your terminal and go to your Craft project:, (*5)
cd /path/to/project
-
Then tell Composer to load the plugin:, (*6)
composer require verbb/bugsnag
-
In the Control Panel, go to Settings → Plugins and click the “Install” button for Bugsnag., (*7)
Configuring Bugsnag
- Copy the config.php configuration file into your
craft/config folder as bugsnag.php.
- Update
serverApiKey with a API key from your Bugsnag project.
- (Optionally) Set the
releaseStage configuration setting to something. Defaults to production.
If you want to be able to capture early initialization errors, you need to add this plugin to your project's bootstrap configuration. To do this, in config/app.php, add the following:, (*8)
'bootstrap' => [
'\verbb\bugsnag\Bootstrap',
]
Blacklisting exceptions
If you want to ignore a certain type of exception, like a 404-error, you can do it like this:, (*9)
<?php
use yii\web\NotFoundHttpException;
return [
'blacklist' => [
[
'label' => '404 errors etc',
'class' => function($exception) {
if ($exception instanceof NotFoundHttpException && $exception->statusCode === 404) {
return false;
}
return true;
},
],
],
];
Using Bugsnag
It will automatically log most exceptions/errors. If you want to log a exceptions/error from an custom plugin, you may use the service methods:, (*10)
- For exceptions:
Bugsnag::$plugin->getService()->handleException($exception);
Using Bugsnag on the frontend
You can log JavaScript errors on your site, by including the following in your Twig templates:, (*11)
{% do view.registerAssetBundle('verbb\\bugsnag\\assetbundles\\frontend\\FrontEndAsset') %}
This currently uses v7.0.0 of the Bugsnag library., (*12)
You also need to set the browserApiKey setting., (*13)
If you'd rather include the Bugsnag client in your build and initialize it yourself, there is a helper method to get the browser config based on your plugin settings:, (*14)
<script>
const bugsnagConfig = { ...{{ craft.bugsnag.getBrowserConfig(true) }} }
</script>
The method takes one parameter, that toggles if it should return JSON or not., (*15)
If you want to send custom metadata with your request, you may do something like this:, (*16)
{% do craft.bugsnag.metadata({ orderId: cart.id }) %}
Note that you have to call these methods before you include the JS bundle., (*17)
Throwing an exception from templates
You can trigger an exception from your templates., (*18)
{% do craft.bugsnag.handleException('Something went terribly wrong.') %}
Credits
Originally created by the team at Superbig., (*19)
Show your Support
Bugsnag is licensed under the MIT license, meaning it will always be free and open source – we love free stuff! If you'd like to show your support to the plugin regardless, Sponsor development., (*20)
, (*21)