Magento 2 Logs having ecommerce store information records for the analysis purpose, as each functionality of ecommerce store either working fine or not, Its both status store in log files One of the most common examples of such events is the error log.
Logs file store path
Magento 2 Root Directory /Var/log/
log folder contains the common log files as below
- system logs — [Magento2 Root//Var/log/system.log]
- exception log — [Magento2 Root//Var/log/exception.log]
- debug logs — [Magento2 Root//Var/log/debug.log]
- connector logs — [Magento2 Root//Var/log/connector.log]
- install logs — [Magento2 Root//Var/log/install.log]
How To Create Custom Log File in Magento 2
There are following below steps need to follow to create Magento 2 custom log file.
Step [1] – Create registration.php file under your module [ Namespace / ModuleName ]
File Path=Mage2db/John/registration.php
Add below content in this file.
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Mage2db_John',
__DIR__
);
Step [2] – Create module.xml file under your module [ Namespace / ModuleName ]
File Path=Mage2db/John/etc/module.xml
Add below content in this file.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mage2db_John" setup_version="1.0.0" />
</config>
Step [3] – Create routes.xml file under your module etc directory. Add below content in this file.
File Path=Mage2db\John\etc\frontend\routes.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="john" frontName="john">
<module name="Mage2db_John" />
</route>
</router>
</config>
Step [4] – Create di.xml file under your module etc directory. Add below content in this file.File Path=Mage2db\John\etc\di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Mage2db\John\Logger\Handler">
<arguments>
<argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
</arguments>
</type>
<type name="Mage2db\John\Logger\Logger">
<arguments>
<argument name="name" xsi:type="string">John</argument>
<argument name="handlers" xsi:type="array">
<item name="system" xsi:type="object">Mage2db\John\Logger\Handler</item>
</argument>
</arguments>
</type>
</config>
Step [5] – Create Handler.php file under your module Logger directory. Here, Custom Log File path store.
Add below content in this file.
File Path=Mage2db\John\Logger\Handler.php
<?php
namespace Mage2db\John\Logger;
use Monolog\Logger;
class Handler extends \Magento\Framework\Logger\Handler\Base
{
protected $loggerType = Logger::INFO;
protected $fileName = '/var/log/mage2db_john_log_file.log';
}
Step [6] – Create Logger.php file under your module Logger directory. Add below content in this file.
File Path=Mage2db\John\Logger\Logger.php
<?php
namespace Mage2db\John\Logger;
class Logger extends \Monolog\Logger
{
}
Step [7] – Create Controller Index.php file under your module Controller Index directory, where custom logger costructor defined.
Here we have defined custom message to store Custom Log File.
Add below content in this file.
File Path=Mage2db/John/Controller/Index/Index.php
<?php
namespace Mage2db\John\Controller\Index;
use Magento\Backend\App\Action\Context;
use \Magento\Framework\Controller\ResultFactory;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\App\Cache\TypeListInterface
*/
protected $_cacheTypeList;
/**
* @var \Magento\Framework\App\Cache\StateInterface
*/
protected $_cacheState;
/**
* @var \Magento\Framework\App\Cache\Frontend\Pool
*/
protected $_cacheFrontendPool;
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* Logging instance
* @var \Mage2db\John\Logger\Logger
*/
protected $_logger;
/**
* @param Action\Context $context
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Framework\App\Cache\StateInterface $cacheState
* @param \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
**/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Cache\StateInterface $cacheState,
\Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Mage2db\John\Logger\Logger $logger
) {
parent::__construct($context);
$this->_cacheTypeList = $cacheTypeList;
$this->_cacheState = $cacheState;
$this->_cacheFrontendPool = $cacheFrontendPool;
$this->resultPageFactory = $resultPageFactory;
$this->_messageManager = $messageManager;
$this->_logger = $logger;
}
/**
* Flush cache storage
*
*/
public function execute()
{
$this->_logger->info("Mage2db.com created custom module for Custom Log");
$this->resultPage = $this->resultPageFactory->create();
return $this->resultPage;
}
}
Step [8] – Finally your custom Log with custom module has been created
Run the following below commands at your Magento 2 root directory
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento indexer:reindex
php bin/magento cache:clean
php bin/magento cache:flush
Run below URL
https://mage2db.com/john/index/index
Change https://mage2db.com to your Base URL, Output as below
Custom Log File store path
Magento 2 Root Directory / var / log / mage2db_john_log_file.txt
[2021-12-07 19:59:58] John.INFO: Mage2db.com created custom module for Custom Log [] []