How to debug Magento 2 events and add logs

In this article we will explore how to inspect and debug information about events that are being fired. We will also take a look on how we can create a log file with all the events.

A better approach would be to use the native Magento 2 profiler to gather this and a lot more information. For practice purposes or when you simply need to quickly gather some logs and can not afford to fire up the profile - this can come in handy.

Lets get started.

The events are dispatches using the interface which is using the following class \Magento\Framework\Event\Manager. We can hook into the before method and add logs to see the events that are dispatched one each request.

First thing we will need to do is declare our plugin. We will define in it the global dependency injection file.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="\Magento\Framework\Event\ManagerInterface">
        <plugin name="TechflarestudioManagerPlugin" type="\Techflarestudio\Logger\Plugin\Manager" sortOrder="1" disabled="false" />
    </type>
</config>
Techflarestudio/Logger/etc/di.xml

Once we have our plugin defined we should create the class.

<?php

namespace Techflarestudio\Logger\Plugin;

use Psr\Log\LoggerInterface;

/**
 * Class ManagerPlugin
 * @package Tecflarestudio\Logger\Event
 */
class Manager
{
    /**
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * ManagerPlugin constructor.
     * @param LoggerInterface $logger
     */
    public function __construct(
        LoggerInterface $logger
    )
    {
        $this->logger = $logger;
    }

    public function beforeDispatch(\Magento\Framework\Event\Manager $subject, $eventName, array $data = [])
    {
        $this->logger->log(200,'EVENT:' . mb_strtolower($eventName));
        return null;
    }
}
\Techflarestudio\Logger\Plugin\Manager

A few things to note in the class:

  • We are creating a before plugin for dispatch() method
  • We are using Psr\Log\LoggerInterface to add logs to native logging system. For developers coming from Magento 1 this is similar to Mage::log() and can be used for custom logging anywhere. The difference is that we have to inject the class using di.
  • Our plugin does not change any arguments so we are returning null

Enable the module, clear caches and you will see logs in your var/log/debug.log

[2021-01-26 20:42:36] main.INFO: EVENT:core_layout_render_element [] []
[2021-01-26 20:42:36] main.INFO: EVENT:core_layout_render_element [] []
[2021-01-26 20:42:36] main.INFO: EVENT:core_layout_render_element [] []
[2021-01-26 20:42:36] main.INFO: EVENT:core_layout_render_element [] []
[2021-01-26 20:42:36] main.INFO: EVENT:catalog_category_collection_add_is_active_filter [] []
[2021-01-26 20:42:36] main.INFO: EVENT:eav_collection_abstract_load_before [] []
[2021-01-26 20:42:36] main.INFO: EVENT:catalog_category_collection_load_before [] []
[2021-01-26 20:42:36] main.INFO: EVENT:catalog_category_collection_load_after [] []
[2021-01-26 20:42:36] main.INFO: EVENT:session_abstract_clear_messages [] []
[2021-01-26 20:42:36] main.INFO: EVENT:controller_front_send_response_before [] []