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();
    }
8
  • 1
    where you are checking your log i.e in which file ?
    – Pawan
    Feb 26, 2020 at 3:06
  • 1
    Please also confirm your run function is executing.
    – Pawan
    Feb 26, 2020 at 3:12
  • Im checking debug log inside magento var/log folder Feb 26, 2020 at 3:21
  • 1
    in var/log which specific file ?
    – Pawan
    Feb 26, 2020 at 3:22
  • 1
    you used info in logger so please check in system.log
    – Pawan
    Feb 26, 2020 at 3:24

4 Answers 4

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');
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');
1
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

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");

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.