0

I want to convert this code from Magento 1 to Magento 2 , it is normally converted like this but I think there is an error

  1. Code In Magento 2 but still Mage::log exist

        foreach ($results['data'] as $data) {
            if ($data->countryCode != $params['countryCode']) {
                Mage::log($data->countryCode . ' - ' . $params['countryCode'], Zend_Log::DEBUG, 'cap.log');
                Mage::log($results, Zend_Log::DEBUG, 'cap.log');
                unset($results['data']);
            }
    
  2. The code checked

    foreach ($results['data'] as $data) {
                if ($data->countryCode != $params['countryCode']) {
                    $this->logger->debug($data->countryCode . ' - ' . $params['countryCode']);
                    $this->logger->debug($results);
                    unset($results['data']);
                }
    

Additional code for better comprehension of the problem

use Psr\Log\LoggerInterface; 
class xxxx extends yyyyy
{
    /**
     * @var  $logger
     * */
    protected $logger;

    /**
     * @var Data
     */
    protected $helper;

    /**
     * xxxx constructor.
     * @param Data $helper
     * @param LoggerInterface $logger
     */
    public function __construct(
        Data $helper,
        LoggerInterface $logger
    ) {
        $this->helper = $helper;
        $this->logger=$logger;
        parent::__construct();
    }

Thanks in advance,

2 Answers 2

0

Did you inject a LoggerInterface in your class and defined $this->logger ?

Otherwise can you provide more information, for example the error you get or the full code of your class ?

5
  • You mean I add logger as a variable like this Commented May 18, 2020 at 14:14
  • Protected LoggerInterface $logger; and add it to the constructor? Commented May 18, 2020 at 14:14
  • exactly, injecting the variables this way is called "constructor dependency injection", if you have done this already and the error still persists, please provide more information if possible. (ah you did already, didnt see it)
    – Ekk4rd
    Commented May 18, 2020 at 14:33
  • if you did what you showed above the logger shouldn't be the issue. Are there any errors relating to this in some of the log files ? Maybe the code with the $logger->debug(); calls is not reached ?
    – Ekk4rd
    Commented May 18, 2020 at 14:37
  • No I am just checking ; I have added the code above after your demand. Commented May 18, 2020 at 14:47
0

check below code

<?php

use Psr\Log\LoggerInterface;

class Save extends Action
{
    private $logger;

    public function __construct(
        Context $context,
        LoggerInterface $logger
    )
    {
        $this->logger = $logger;
        parent::__construct($context);
    }

}

write below code to write a proper array format in your log file.

$this->_logger->log(100,print_r($results,true));

or

$this->logger->info(print_r($results, true));

write below code to write a proper string in your log file.

$this->logger->info('countryCode'.$params['countryCode']);

try to implement the below code in your code.

foreach ($results['data'] as $data) {
        if ($data->countryCode != $params['countryCode']) {
            $this->logger->info($data->countryCode.$params['countryCode']);
            $this->logger->log(100,print_r($results,true));
            unset($results['data']);
        }

I hope this will help you.

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.