1

I have added like below way. But it doesn't work for that module

use Psr\Log\LoggerInterface;

/**
 * @var LoggerInterface
 */
protected $logger;

protected $resultForwardFactory;

public function __construct(
    Action\Context $context,
    \Magento\Framework\Registry $coreRegistry,
    \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
    \Magento\Framework\Translate\InlineInterface $translateInline,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
    \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
    \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
    \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
    LoggerInterface $logger
) {
    $this->_coreRegistry = $coreRegistry;
    $this->_fileFactory = $fileFactory;
    $this->_translateInline = $translateInline;
    $this->resultPageFactory = $resultPageFactory;
    $this->resultJsonFactory = $resultJsonFactory;
    $this->resultLayoutFactory = $resultLayoutFactory;
    $this->resultRawFactory = $resultRawFactory;
    $this->resultForwardFactory = $resultForwardFactory;

    $this->logger = $logger;
    parent::__construct($context);
}




 /**
     * @return void
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Exception
     */
    public function run()
    {
        $this->resource->getConnection()->beginTransaction();
        try {
            $this->_prepare();
            $this->_execute();
            $this->resource->getConnection()->commit();
            $this->logger->info('Test');

        } catch (\Exception $e) {
            $this->resource->getConnection()->rollBack();
            $this->logger->info($e);
            throw $e;
        }
        $this->getFlag()->save();
    }

4 Answers4

3

you can do this simple way ..

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your text message');
Rakesh Donga
  • 5,344
  • 2
  • 24
  • 57
2

Your code seems correct to me.

if your run function is executing, there should be log present in log file.

As per my observation you are looking in wrong log file.

if you added $this->logger->info('Test');, Your log should be present in system.log.

for more info please look at this

Pawan
  • 5,856
  • 2
  • 14
  • 36
1

You can use any of the following methods. sure one of them will work.

**Method 1:**
$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/customfile.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('I am logged from Zend_log_writer');

Method 2:

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/logfileZend.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Simple Text Log from Zend logger 2'); // Simple Text Log

Method 3:

$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/customlaminas.log');
$logger = new \Laminas\Log\Logger();
$logger->addWriter($writer);
$logger->info("log from laminas log");

0

Use the following code for Magento versions prior to 2.4.2.

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

Use the following code for Magento 2.4.2

$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/custom.log');
$logger = new  \Laminas\Log\Logger();
$logger->addWriter($writer);
$logger->info('text message');

Use the following code for Magento 2.4.3

$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('text message');
Monark Bhawani
  • 654
  • 1
  • 4
  • 12