---
title: Error Reporting with Rollbar and Craft CMS
date: 2024-08-06T22:02:00+01:00
author: John Henry Donovan
canonical_url: "https://johnhenry.ie/blog/2024/08/error-reporting-with-rollbar-and-craft-cms"
section: Blog
---
Browse by categoryAll PostsWeb DevelopmentDesignLifestyleMusic &amp; FilmTutorialsSpottedSpeakingReviewsFound FoodRecipesIreland

[](/blog)

# Error Reporting With Rollbar And Craft CMS

Posted on 06th August, 2024

Rollbar, a third-party service, provides an efficient way to log errors from Craft CMS without requiring a plugin. This streamlined approach can enhance the efficiency of your error-logging tasks.

## What is Rollbar?

[Rollbar](https://rollbar.com/) 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 ](https://rollbar.com/signup/)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`

```twig
ROLLBAR_ACCESS_TOKEN_SERVER=*************
```

.env

## Add Rollbar to your composer.json

Next step is to add the Rollbar library to your project’s `composer.json`

```twig
ddev composer require rollbar/rollbar
```

## Configure Craft CMS

Update the Craft CMS `config/app.php` file.

```php
// 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);
    }
}
```

/config/app.php

## Resources

- [https://​docs​.rollbar​.com/​d​o​c​s​/​m​o​n​o​l​o​g​#​m​o​n​o​l​og-2x](https://docs.rollbar.com/docs/monolog#monolog-2x)
- [https://​craftcms​.com/​d​o​c​s​/​5​.​x​/​r​e​f​e​r​e​n​c​e​/​c​o​n​f​i​g​/​a​p​p​.html](https://craftcms.com/docs/5.x/reference/config/app.html)
- [https://​github​.com/​S​e​l​d​a​e​k​/​m​o​n​o​l​o​g​/​b​l​o​b​/​m​a​i​n​/​s​r​c​/​M​o​n​o​l​o​g​/​H​a​n​d​l​e​r​/​R​o​l​l​b​a​r​H​a​n​d​l​e​r.php](https://github.com/Seldaek/monolog/blob/main/src/Monolog/Handler/RollbarHandler.php)
- [https://​docs​.rollbar​.com/​d​o​c​s​/​g​e​t​t​i​n​g​-​s​t​arted](https://docs.rollbar.com/docs/getting-started)
- [https://​craftcms​.com/​d​o​c​s​/​5​.​x​/​s​y​s​t​e​m​/​l​o​g​g​i​n​g​.html](https://craftcms.com/docs/5.x/system/logging.html)

### Share this link via

###### Or copy link

Copy

[ Previous Blog Post

How Craft CMS Built Craft Cloud](https://johnhenry.ie/blog/2024/08/how-craft-cms-built-craft-cloud "Previous Blog Post")

[Next Blog Post

A Better Title Filter For Twig Us…](https://johnhenry.ie/blog/2024/08/a-better-title-filter-for-twig-using-craft-cms "Next Blog Post")
