Logging to custom file is not working from module helper
-
30-09-2020 - |
Question
I wanted to wrap the logging functionality in my module helper.
I have defined Custom Logger as:
File: app/code/MagePsycho/CustomLogger/etc/di.xml
<?xml version="1.0"?>
<!--
/**
* @category MagePsycho
* @package MagePsycho_CustomLogger
* @author magepsycho@gmail.com
* @website http://www.magepsycho.com
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="MagePsychoCustomLogger" type="Magento\Framework\Logger\Monolog">
<arguments>
<argument name="handlers" xsi:type="array">
<item name="debug" xsi:type="object">MagePsycho\CustomLogger\Logger\Handler\Custom</item>
</argument>
</arguments>
</virtualType>
<!-- DI for Module Observer -->
<type name="MagePsycho\CustomLogger\Observer\ControllerActionPredispatch">
<arguments>
<argument name="logger" xsi:type="object">MagePsychoCustomLogger</argument>
</arguments>
</type>
<!-- DI for Module Helper -->
<type name="MagePsycho\CustomLogger\Helper\Data">
<arguments>
<argument name="logger" xsi:type="object">MagePsychoCustomLogger</argument>
</arguments>
</type>
</config>
File: app/code/MagePsycho/CustomLogger/Helper/Data.php
<?php
namespace MagePsycho\CustomLogger\Helper;
/**
* @category MagePsycho
* @package MagePsycho_CustomLogger
* @author magepsycho@gmail.com
* @website http://www.magepsycho.com
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $_logger;
/**
* @param \Magento\Framework\App\Helper\Context $context
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Module\ModuleListInterface $moduleList
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Module\ModuleListInterface $moduleList
) {
$this->_logger = $logger; //$context->getLogger();
$this->_moduleList = $moduleList;
parent::__construct($context);
}
/**
* Logging Utility
*
* @param $message
* @param bool|false $useSeparator
*/
public function log($message, $useSeparator = false)
{
//Even this is not working
#$this->_logger->pushHandler( new \Monolog\Handler\StreamHandler( '/var/log/magepsycho_customlogger.log') );
if ($useSeparator) {
$this->_logger->addDebug(str_repeat('=', 100));
}
$this->_logger->addDebug($message);
}
}
And trying to use as (from any observer, model, block classes)
/**
* @var \MagePsycho\CustomLogger\Helper
*/
protected $_helper;
public function __construct(
\MagePsycho\CustomLogger\Helper\Data $helper
) {
$this->_helper = $helper;
}
And call it from any methods
$this->_helper->log('message to be logged...');
But this is always logging to var/log/debug.log
file.
If I use directly use the logger as DI, it works. But I wanted the logging functionality via Helper class.
Any idea what am I doing wrong?
You can find the sample module that I am using for custom logger: https://github.com/MagePsycho/magento2-custom-logger
Solution
I was able to fix the issue.
The issue was related with the Constructor DI $logger
in \MagePsycho\CustomLogger\Helper\Data
.
Solution:
Renaming the $logger
to $customLogger
in the Helper class and di.xml fixed the issue.
Updated working code can be found at: https://github.com/MagePsycho/magento2-custom-logger
OTHER TIPS
I confirm the issue
I debuged the code source of the extension then i found that when you called
$this->_helper->log('something', true);
from an other class
$this->_helper call the class Magento\Backend\Helper\Data not the \MagePsycho\CustomLogger\Helper\Data
I solved the problem by renaming $_helper var to something else.
To resolve your problem
uncomment this line
$this->_logger->pushHandler( new \Monolog\Handler\StreamHandler( '/var/log/magepsycho_customlogger.log') );
in this file \MagePsycho\CustomLogger\Helper\Data
to be like
$this->_logger->pushHandler( new \Monolog\Handler\StreamHandler( 'var/log/magepsycho_customlogger.log') );
NB: There is an other solution more simple to use custom logs