I am programmatically saving the products in Magento backend reading from CSV. How to log if SKU already exist or not if the product failure or success need to log in Magento. How to achieve this.

You can use the Psr\Log\LoggerInterface in your file to log data or custom message like below example.

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

then use debug, exception, system for in Logger to display message :

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

if you are not going to inject than you can use with objectmanager also like:

\Magento\Framework\App\ObjectManager::getInstance()
    ->get('Psr\Log\LoggerInterface')->debug('message');

Another way is to use zend library to print logs like

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('message'); 
$logger->info('Array Log'.print_r($myArray, true));

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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