How to log using FirePHP in Magento 2

Likes (1)   Dislikes (0)  

As a developer logging crucial debug information is an unavoidable part of our coding process. We all do write logs at places in our code for later use and investigation. These logs help us to understand what went wrong in some parts of the code so that we can debug that section of the code to fix issues. Also, we write logs to monitor essential things in the code base that we develop.

The problem with file-based/cloud logging

Usually, we write logs to files. As time passes the log files grow in size and hence looking for a particular piece of information became difficult. Also, the same thing happens in the case of cloud logging services. Finding a piece of information in the log became a headache. Things even went more critical when you don’t have access to the cloud logging service. You have to wait for someone else to provide you with the proper piece of log that will be helpful for you to resolve an issue.

Solution for instant logging

Sometimes we need to write logs in a use-and-throw fashion. We want to quickly log something that can be accessed easily. These logs are just meant for debugging purposes and they are temporary logs. Once we are done with the debugging process, we do not need those logs. So for easy access to logs, we have to think of something that is more like browser console logs. They are instantly available to debug, no need to search for a large log file. FirePHP is one such tool (browser extension) that helps us to write PHP logs to the browser console. So let’s integrate FirePHP in Magento 2.

Configuring FirePHP in Magento 2

The Monolog PHP package is built into Magento 2 for logging purposes. It provides the factory design pattern to create an instance of its type. We can leverage this factory pattern to inject a log handler of our choice. And that is what we did in the below code block, we injected the FirePHPHandler into the Monolog Logger Factory create method.



namespace Training\FirePhp\Logging;

use Monolog\Handler\FirePHPHandler;

class Logger
    private \Monolog\Logger $logger;

    public function __construct(\Monolog\LoggerFactory $loggerFactory)
        $this->logger = $loggerFactory->create([
            'name' => 'fire_php', // this could be anything you like
            'handlers' => [new FirePHPHandler()]

     * @return \Monolog\Logger
    private function getLogger(): \Monolog\Logger
        return $this->logger;

    public function debug(string $message, array $context = []): void
        $this->getLogger()->debug($message, $context);

The custom class that we defined will hold the internal logic of creating a logger instance. We will provide a public interface for different logging functions. The responsibility of getting the actual logger instance will also be hidden. With this custom class, FirePHP is now configured in Magento 2. So let’s see how to integrate the FirePHP browser extension to handle Magento 2 logs. This is the way for linking Magento 2 logs to the FirePHP extension.

Integrating FirePHP with Magento 2

Just need to install the FirePHP extension for your preferred browser and you are good to go for debugging PHP logs in your browser developer console.

Using The FirePHPLogger in Magento 2

Now we can use this custom logger class for instant temporary logging in the browser developer console. Just update the generated code in Magento 2 and then start injecting the new logger class to any of the required classes.

Leave a Comment

Share via
Copy link
Powered by Social Snap