0

I have entered data in the database using object Manager in Model file itself. I want to print a log file from the plugin that will print the log of the last data that is inserted in the database.

  • Yes I checked that post. but could not solve my problem – Naiwrita09 Aug 22 '19 at 8:34
  • you need to print last inserted data in your custom named log file right ? – Shafeel Sha Aug 22 '19 at 8:42
  • yes. fetch the data from the database using id and print the log and this has to be done in Plugin file – Naiwrita09 Aug 22 '19 at 8:45
  • please provide the data fetching code ? – Shafeel Sha Aug 22 '19 at 8:46
1

To generate log file you can use below code. add pass data as per your need.

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

For more information read this. Magento 2: Replacement for Mage::log method?

  • Actually I want to know the fetching of data. how to get the id and print the data accordingly. – Naiwrita09 Aug 22 '19 at 8:33
  • Hi @Naiwrita09, Please check my comment and see if that can help – Rahul Anand Aug 22 '19 at 9:27
  • ok I will check and let you know @RahulAnand – Naiwrita09 Aug 22 '19 at 9:30
0

Your need to create a plugin for "Magento\Framework\Model\AbstractModel" for "afterSave" method and log all the logs in that. AbstractModel executes all the time for all the model save.

In your di.xml file declare the plugin:

<type name="Magento\Framework\Model\AbstractModel">
<plugin name="log_query" type="Magento\ExternalLinks\Plugin\logQuery" sortOrder="1" />

in your plugin file log the data.

namespace Magento\ExternalLinks\Plugin;

class LogQuery
{
    public function afterSave(\Magento\Framework\Model\AbstractModel $subject, $result)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/logfile.log');
         $logger = new \Zend\Log\Logger();
         $logger->addWriter($writer);
         $logger->info(var_dump($result));
        return $result;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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