PSR Logger not working for Magento 2
-
04-10-2020 - |
سؤال
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
المحلول
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;
}
نصائح أخرى
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);