What is Rollbar?
Rollbar is an error tracking and monitoring tool designed to help developers identify, track, and manage errors and exceptions in their applications. It provides real-time notifications and insights into issues, allowing development teams to quickly diagnose and fix problems.
Setting Up Rollbar
Rollbar offers a free plan to get set up. During the onboarding process, they will ask a few questions about your integrations. Choose the PHP SDK
when asked in the project setup.
Once you get your Project Access Token, the post_server_item
specifically, then add it to a new .env
variable in your craft install called ROLLBAR_ACCESS_TOKEN_SERVER
ROLLBAR_ACCESS_TOKEN_SERVER=*************
Add Rollbar to your composer.json
Next step is to add the Rollbar library to your project's composer.json
ddev composer require rollbar/rollbar
Configure Craft CMS
Update the Craft CMS config/app.php
file.
// php opening tag
use craft\helpers\App;
use Monolog\Logger;
use Monolog\Handler\RollbarHandler;
use Rollbar\Rollbar;
use samdark\log\PsrTarget;
// Rollbar Logging Setup
try {
initRollbarLogging();
} catch (Exception $e) {
}
return [
'id' => App::env('CRAFT_APP_ID') ?: 'CraftCMS',
'components' => [
'log' => [
'targets' => [
[
'class' => PsrTarget::class,
// Don't log any 404 errors
'except' => [
'yii\web\HttpException:40*',
],
'logger' => (
new Logger('web'))->pushHandler(
new RollbarHandler(Rollbar::logger()), Logger::DEBUG),
'addTimestampToContext' => true,
'logVars' => [],
]
]
],
],
];
function initRollbarLogging()
{
if (App::env("ROLLBAR_ACCESS_TOKEN_SERVER")) {
$rollbarConfig = [
"access_token" => App::env("ROLLBAR_ACCESS_TOKEN_SERVER"),
"environment" => App::env('CRAFT_ENVIRONMENT'),
// Optional for a Craft CMS site with users
"capture_email" => true,
"capture_username" => true,
"person_fn" => static function () {
if ($user = Craft::$app->getUser()->getIdentity()) {
return [
"id" => $user->id,
"username" => $user->email,
"email" => $user->email,
];
}
return null;
},
];
Rollbar::init($rollbarConfig);
}
}