Today we will check how to add Custom Logs in Magento 2. You can add custom logs by using the built-in logging functionality In Magento 2. We have two methods to add or Create a Custom Log in Magento 2 and they are covered below.
How to Add Custom Logs in Magento 2
First Method:
Assuming your module is already created and has a namespace as something like Vendor/Module:
1) Write Logger class in Logger/Logger.php:
<?php
namespace Vendor\Module\Logger;
class Logger extends \Monolog\Logger
{
}
2) Write Handler class in Logger/Handler.php:
The below handler class defines the custom log file along with its relative path, starting from the Magento root directory. In this file, you can give a unique name to the handler. In my example case, it is set to “customLog.log”.
<?php
namespace Vendor\Module\Logger;
use Monolog\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/customLog.log';
}
Note: This is the only step that uses the Magento code. \Magento\Framework\Logger\Handler\Base extends Monolog’s StreamHandler and e.g. prepends the $fileName attribute with the Magento base path.
3) Register Logger in Dependency Injection. Create or add in etc/di.xml:
Now, you need to define the custom logger handler information in your di.xml file. The handler Vendor\Module\Logger\Handler.php will handle your logger class, which is Vendor\Module\Logger\Logger.php.
<?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="Vendor\Module\Logger\Handler">
<arguments>
<argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
</arguments>
</type>
<type name="Vendor\Module\Logger\Logger">
<arguments>
<argument name="name" xsi:type="string">customLoggerName</argument>
<argument name="handlers" xsi:type="array">
<item name="system" xsi:type="object">Vendor\Module\Logger\Handler</item>
</argument>
</arguments>
</type>
</config>
Note: This is not strictly required but allows the Dependency Injection to pass specific arguments to the constructor. If you do not do this step, then you need to adjust the constructor to set the handler.
4) Use the logger in your Magento classes:
This is done by Dependency Injection. Below you will find a class that only writes a log entry:
<?php
namespace Vendor\Module\Model;
class CustomController {
/**
* Logging instance
* @var \Vendor\Module\Logger\Logger
*/
protected $logger;
/**
* Constructor
* @param \Vendor\Module\Logger\Logger $logger
*/
public function __construct(
\Vendor\Module\Logger\Logger $logger
) {
$this->logger = $logger;
}
public function addLogText()
{
$this->logger->info('This is a dummy text for log.');
}
}
Second Method:
We can also do it directly in files without the above procedure and it is a slightly easier way, especially if you want to incorporate that into multiple modules or if you want different log files within your module. With this method, you don’t have to create custom handlers.
For Magento 2.4.2 or before the version, we need to use the below code snippet:
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/customLog.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Custom message');
$logger->info(print_r($object->getData(), true));
For Magento 2.4.2 or after the version, we need to use the below code snippet:
$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/customLog.log');
$logger = new \Laminas\Log\Logger();
$logger->addWriter($writer);
$logger->info('text message');
$logger->info(print_r($object->getData(), true));
For Magento 2.4.3 version, we need to use the below code snippet:
$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/customLog.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('text message');
$logger->info(print_r($object->getData(), true));
Now, if you go to the /var/log directory of your Magento 2 setup, you can see your custom log file customLog.log has been generated.
Congratulations! You have just created a custom logger in Magento 2 and hope this will help you to organize your custom Magento logs in a more meaningful manner and have got how to create a custom log in Magento 2.
Also, check more on Magento 2 Logs here https://www.cloudways.com/blog/magento-logs/
Also Read: https://techurbane.com/must-have-magento-2-extensions-supercharge-your-store/
I hope this helps! If you have any further questions, please don’t hesitate to reach out.