Magento2 Custom Log File Using Monolog
Magento 2 Custom Log File Using Monolog – Creating a custom module in magento 2 often requires to identify the errors and information logs for various reasons such as debugging .
But it is difficult to find the relevant information that is required in one single log file. Therefore, we should always create a custom log file for every module in Magento.
If you are having issue with creating your own custom log file then you can find the solution here in this blog,here you can learn how to create your own custom log file.
Here I am going to create a custom log file named “customfile.log” file using Default Magento 2 logger (Monolog), so now I am going to explain step by step please follow the steps below to create one of your own.
Step 1 : Declaration of Logger and handlers in Magento 2 custom module
Create a file di.xml by following the path your_magento_root/app/code/Webkul/Modulename/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Webkul\Modulename\Logger\Handler">
<arguments>
<argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
</arguments>
</type>
<type name="Webkul\Modulename\Logger\Logger">
<arguments>
<argument name="name" xsi:type="string">customLogHandler</argument>
<argument name="handlers" xsi:type="array">
<item name="system" xsi:type="object">Webkul\Modulename\Logger\Handler</item>
</argument>
</arguments>
</type>
</config>
Step 2 : Create a file Handler.php at Webkul\Modulename\Logger\ which will extend \Magento\Framework\Logger\Handler\Base class
Here I am defining the custom log file name as “customfile.log” in Handler class
<?php
namespace Webkul\Modulename\Logger;
class Handler extends \Magento\Framework\Logger\Handler\Base
{
/**
* Logging level
* @var int
*/
protected $loggerType = Logger::INFO;
/**
* File name
* @var string
*/
protected $fileName = '/var/log/customfile.log';
}
Step 3 : Create a file Logger.php at following path Webkul\Modulename\Logger\ which will extend the Monolog\Logger class
<?php
namespace Webkul\Modulename\Logger;
class Logger extends \Monolog\Logger
{
}
Step 4 : log any test data in your log file “customfile.log” by calling your custom Magento 2 Controller or Helper method, Here I am using Controller to log data in my custom log file
<?php
namespace Webkul\Modulename\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
class Index extends Action
{
/**
* Logging instance
* @var \Webkul\Modulename\Logger\Logger
*/
protected $_logger;
/**
* @param Context $context
* @param \Webkul\Modulename\Logger\Logger $logger
*/
public function __construct(
Context $context,
\Webkul\Modulename\Logger\Logger $logger
) {
$this->_logger = $logger;
parent::__construct($context);
}
/**
* Default customer account page
*
* @return \Magento\Framework\View\Result\Page
*/
public function execute()
{
$data = $this->getRequest()->getParams();
$this->_logger->info(print_r($data)); // log array Data to customfile.log
$this->_logger->info("Some text string data"); // log string Data to customfile.log
// Write Your Code Here
return $this->resultRedirectFactory->create()->setPath('*/*/*');
}
}
As you can see we have to call the logger methods to create a log entry in the custom log file.
Here I have used info() method.
Other methods that you can use are :
$this->_logger->warning()
$this->_logger->error()
So in this way you can create your own log file. 🙂
You can also check the below links :