How to Monitor Errors in Magento 2 the Easy Way

How to Monitor Errors in Magento 2 the Easy Way

October 25th, 2018 • By Jason Skowronski

Magento is the largest open-source eCommerce platform in the world, used by more than 250,000 merchants. While Magento makes setting up a store easier, there is complexity with custom plugins and extensions, and custom site behavior. Sophisticated stores also introduce more opportunity for problems with errors, bugs, performance, and more. A critical production issue might not just cost you a sale, but also customer trust. Errors can affect your business in surprising ways. To avoid impacting customers, your team needs visibility into problems that affect user experience to fix issues as quickly as possible.

In this post, we’ll show you how to enable exception printing and monitor errors in Magento 2 applications, both using the native exception log and the popular error monitoring solution Rollbar. Rollbar will give you an overview of all the errors happening on your store, how many customers are affected, and debugging information to find the root cause.

We will walk you through the process of setting up Rollbar with Magento 2. We’ve created an example app that will trigger an exception when the user clicks on a button. The error message is recorded on Rollbar with a stack trace so you can see the exact line of code that caused the error.

Rollbar loves Magento

Native exception handling in Magento 2

Magento 2 gives you exception logs in a file by default. If you get an error message that says “There has been an error processing your request, Magento exception printing is disabled by default for security reasons”, then verify that your server has write permissions to the log directory. You can then enable exception printing by going to System > Configuration > Developer > Log Settings and setting Enabled = Yes.

You can normally locate your logs in {project root}\var\log\exception.log. Below, you can see an example exception log where each record number includes the date and time of logging, log level, and exception details.

[2018-09-11 12:20:03] main.CRITICAL: Warning: Creating default object from empty value in D:\xampp\htdocs\magento\app\code\Rollbar\Custom\Controller\Custom\index.php on line 12 {"exception":"[object] (Exception(code: 0): Warning: Creating default object from empty value in D:\\xampp\\htdocs\\magento\\app\\code\\Rollbar\\Custom\\Controller\\Custom\\index.php on line 12 at D:\\xampp\\htdocs\\magento\\vendor\\magento\\framework\\App\\ErrorHandler.php:61)"} []

Magento 2’s default error handler can be found in {project root}\vendor\magento\framework\App\ErrorHandler.php. You can extend this error handler with your own logic to customize the behavior.

namespace Magento\Framework\App;
class ErrorHandler{
    protected $errorPhrases = [
        E_ERROR => 'Error',
        E_WARNING => 'Warning',
        E_PARSE => 'Parse Error',
        E_NOTICE => 'Notice',
        E_CORE_ERROR => 'Core Error',
        E_CORE_WARNING => 'Core Warning',
        E_COMPILE_ERROR => 'Compile Error',
        E_COMPILE_WARNING => 'Compile Warning',
        E_USER_ERROR => 'User Error',
        E_USER_WARNING => 'User Warning',
        E_USER_NOTICE => 'User Notice',
        E_STRICT => 'Strict Notice',
        E_RECOVERABLE_ERROR => 'Recoverable Error',
        E_DEPRECATED => 'Deprecated Functionality',
        E_USER_DEPRECATED => 'User Deprecated Functionality',

    public function handler($errorNo, $errorStr, $errorFile, $errorLine)
        if (strpos($errorStr, 'DateTimeZone::__construct') !== false) {
        // there's no way to distinguish between caught system exceptions and warnings
        return false;

    $errorNo = $errorNo & error_reporting();
    if ($errorNo == 0) {
        return false;

    $msg = isset($this->errorPhrases[$errorNo]) ?
        $this->errorPhrases[$errorNo] : "Unknown error ({$errorNo})";
    $msg .= ": {$errorStr} in {$errorFile} on line {$errorLine}";
    throw new \Exception($msg);

To customize the log format, just make modifications in the $msg of the handler function. You can find this function under the ErrorHandler class.

$msg = isset($this->errorPhrases[$errorNo]) ?
        $this->errorPhrases[$errorNo] : "Unknown error ({$errorNo})";
$msg .= ": {$errorStr} in {$errorFile} on line {$errorLine}";

Enabling printing of this exception log is very convenient for small deployments running on a single server. However, for large stores with multiple servers it can be difficult to aggregate and analyze all the logs. This makes it difficult to prioritize problems and identify the root cause in order to fix them.

Monitoring Magento 2 errors with Rollbar

Rollbar is an error monitoring solution that gives you an easier summary of the errors happening on your site and helps you prioritize the most critical errors. It provides you with real-time feed and instant alerts as the errors appear. It automatically tracks and prioritizes the errors, offering full stack visibility on the web and server side. With Rollbar, you can find the root cause of the errors easier by providing the stack trace which shows the line of code that caused the error, alongside extra contextual information like which type of users are affected, which browsers they are using, and more.

We’ll show you how to set up Magento 2 to send errors to Rollbar with the help of an example app that will trigger an exception when the user clicks the button.

Video of Rollbar magento 2 example

How to set up a Magento project on Rollbar

To add Magento to Rollbar, follow these steps:

  1. Visit and sign up for an account if you haven’t done so yet. Next, create your project and select Backend from the list of notifiers. Select the client-side access token that is generated for you. You’ll need this to configure Rollbar in the steps below.

  2. Open the command prompt under the root project directory and run the script below.

php bin/magento setup:upgrade
  1. Find composer.json in the root project directory and add the following dependency.
 "rollbar/rollbar-magento2": "*"

Now, you need to generate the autoloading file. Open the command prompt under root project and run the command below to generate the autoloading file.

composer update
  1. Configure Rollbar in your app/etc/env.php and replace the post access token with your access token. You can also change the environment at your convenience.
'rollbar' => [
    'access_token' => 'POST-ACCESS-TOKEN',
    'environment' => 'development'
  1. Enable the Rollbar Magento SDK by opening the command prompt under the root project directory and running the following command.
php bin/magento module:enable Rollbar_Magento2
  1. Finally, upgrade the Magento setup by running the command below.
php bin/magento setup:upgrade

Test Rollbar with an example Magento 2 app

In the example below, we’ve created a page titled "Magento Rollbar" on the Magento dashboard. You can generate an error by clicking the “Generate Uncaught Error” button.

Magento 2 error page

Here is the HTML code for the Magento Rollbar page above.

<div class="col-sm-15 mainContainer ">
    <p style="text-align: center;">
        <span style="font-size: xx-large;">
        <strong>Rollbar Magento-2 Example</strong></span>
    <p style="text-align: center;">
        <a href="">
            <button class="btn btn-danger">Generate Uncaught Error</button>

When you click the "Generate Uncaught Error" button it triggers the execute() method of the controller. Here, we have a bug in the code which generates an exception saying “Exception: Warning: Creating default object from empty value”.

namespace Rollbar\Custom\Controller\Custom;

Class Uncaught extends \Magento\Framework\App\Action\Action {

    public function __construct(\Magento\Framework\App\Action\Context $context) {

    public function execute() {
        $x = null;
        $x->foo = 5; // this creates an error

Viewing errors in Rollbar

Open the account’s item page in Rollbar to see what these errors look like. You can click on the items to get more details of the errors. The error we’ve generated in this example should be called "Exception: Warning: Creating default object from empty value." This page shows error items, which includes both error log records and informational messages, if you choose to send them.

Screenshot of Rollbar error item

We have also attached the traceback that shows the exact source code file, method, and line number responsible for generating the error.

Screenshot of Rollbar error item detail


It’s really easy to track errors that could affect user experience using Rollbar. This will give you visibility into site problems that may be causing you to lose sales and repeat business. Rollbar collects data about every error including which lines of code, which user is affected, and more. You can then send alerts and use the data to debug problems faster. With Rollbar, it takes only a few minutes to set up, trace, and debug the errors in Magento 2.

If you haven’t already, sign up for a 14-day free trial of Rollbar and let us help you take control of your Magento 2 errors.

Get the latest updates delivered to your inbox, once a month.