Sunday 2 April 2017

Custom logger in magento 2 | FirePhp | Console log

In my previous post I have explained how to use logger in Magento 2. This post details using FirePHP logging in Magento 2.

This awesome logging will output the Log data on your Browser console. I have tested on Chrome & Firefox. In this post I will be referencing Chrome.

Step 1:
Install Extension for Chrome.
You need to install two extensions, one is regular logging and Other is for logging during Ajax requests.

Step 2:
Now we need to add our codes to ignore the regular logging to files and output the logs on our browser console.
Creating Logger instance using DI is explained in previous post. Please go though if you haven't yet.
Magento 2 includes FirePHP by default. We just need to call it for logging.

After creating the $this->_logger Object. use below code.

$this->_logger->pushHandler(new \Monolog\Handler\FirePHPHandler());
$this->_logger->addDebug('Debug log');

There are multiple types for logging.

$this->_logger->addInfo();
$this->_logger->addNotice();
$this->_logger->addError();
$this->_logger->addWarning();
$this->_logger->addEmergency();
$this->_logger->addCritical();
$this->_logger->addAlert();

NOTE: If you want to log an array(), you need to pass it as a second argument which is optional.

$this->_logger->addDebug('Debug array', $arrayVar);

Example output