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 [email protected]
* @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 [email protected]
* @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