2

I'm trying to log some info in the existing Magento 2 module just to check if the code is running or not. Following files have been modified to log info.

Magento\Checkout\Block\Cart\Item\Renderer.php

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Checkout\Block\Cart\Item;
.
.
.
.
use Psr\Log\LoggerInterface;
.
.
.
protected $_logger;

 public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        .
        .
        LoggerInterface $logger,
        array $data = []
    ) {
        .
        .
        .
        $this->logger = $logger;
        parent::__construct($context, $data);
        .
        .
        .

    }
.
.
.
public function getProductOptions()
    {
        /* @var $helper \Magento\Catalog\Helper\Product\Configuration */
        $helper = $this->_productConfig;
        $this->_logger->debug('Product Options');
        return $helper->getCustomOptions($this->getItem());
    }

I'm getting following error.

Exception #0 (Exception): Recoverable Error: Argument 10 passed to Magento\Checkout\Block\Cart\Item\Renderer::__construct() must implement interface Psr\Log\LoggerInterface, array given, called in /Applications/MAMP/htdocs/magentoce21/vendor/magento/module-bundle/Block/Checkout/Cart/Item/Renderer.php on line 65 and defined in /Applications/MAMP/htdocs/magentoce21/vendor/magento/module-checkout/Block/Cart/Item/Renderer.php on line 120

3
  • Had you a try with my suggested answer ? Nov 19, 2016 at 19:09
  • yes, that's what I did at the start and got stuck and posted this question, I have accepted Siarheys' answer as it worked out
    – Nausif
    Nov 22, 2016 at 7:00
  • You can also check my answer for the issue. Nov 22, 2016 at 7:02

2 Answers 2

3

You should refresh generation files. Try to remove the var/generation directory content, like:

rm -rf var/generation/*

from the magento root directory.

Additional info: you can use logger from the parent class, without declaring new logger, becouse it already exists in the _logger parameter.

Update: It is possible that error is coming from the some other block which extends your block, but still uses a standard set of arguments in the __construct method when it try to call the parent __construct method.

I think this block located here:

vendor/magento/module-bundle/Block/Checkout/Cart/Item/Renderer.php

class Renderer extends \Magento\Checkout\Block\Cart\Item\Renderer // here is your block

.....

/**
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param \Magento\Catalog\Helper\Product\Configuration $productConfig
 * @param \Magento\Checkout\Model\Session $checkoutSession
 * @param \Magento\Catalog\Block\Product\ImageBuilder|\Magento\Catalog\Helper\Image $imageBuilder
 * @param \Magento\Framework\Url\Helper\Data $urlHelper
 * @param \Magento\Framework\Message\ManagerInterface $messageManager
 * @param PriceCurrencyInterface $priceCurrency
 * @param \Magento\Framework\Module\Manager $moduleManager
 * @param InterpretationStrategyInterface $messageInterpretationStrategy
 * @param Configuration $bundleProductConfiguration
 * @param array $data
 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
 */
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Helper\Product\Configuration $productConfig,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Catalog\Block\Product\ImageBuilder $imageBuilder,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    \Magento\Framework\Message\ManagerInterface $messageManager,
    PriceCurrencyInterface $priceCurrency,
    \Magento\Framework\Module\Manager $moduleManager,
    InterpretationStrategyInterface $messageInterpretationStrategy,
    Configuration $bundleProductConfiguration,
    array $data = []
) {
    $this->_bundleProductConfiguration = $bundleProductConfiguration;
    parent::__construct(
        $context,
        $productConfig,
        $checkoutSession,
        $imageBuilder,
        $urlHelper,
        $messageManager,
        $priceCurrency,
        $moduleManager,
        $messageInterpretationStrategy,
        $data // 10 is data-array , this should be your logger and next you should add the $data (11 argument)
    );
    $this->_isScopePrivate = true;
}
3
  • Did it, still not working
    – Nausif
    Nov 19, 2016 at 10:23
  • @Nausif please, see updated answer. I'll recomend you to use the already defined _logger ($this->_logger) Nov 19, 2016 at 10:34
  • 1
    Thanks Siarhey! It works using parent's _logger parameter.
    – Nausif
    Nov 19, 2016 at 11:08
4

See below example of print log

protected $logger;
public function __construct(\Psr\Log\LoggerInterface $logger)
{
    $this->logger = $logger;
}

You use debug, exception, system for psr logger for example

$this->logger->info($message);
$this->logger->debug($message);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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