2

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

3

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

1

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

Logs and Magento 2

8
  • I renamed the $_helper in Observer to $_customLoggerHelper but still not working for me.
    – MagePsycho
    Jun 30 '16 at 15:18
  • 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') );
    – Dayssam
    Jun 30 '16 at 15:31
  • var/log/... not /var/log/...
    – Dayssam
    Jun 30 '16 at 15:32
  • Event that didn't help. but full path(absolute path) did help.
    – MagePsycho
    Jun 30 '16 at 15:34
  • It works for me with var/log/magepsycho_customlogger.log
    – Dayssam
    Jun 30 '16 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.