3

I am trying to print order details in Log /return JSON result when a new order is placed. Nothing is printing in System.log or in exception.log,

Please check my code below, Let me know where I am doing wrong.

\app\code\Autosynch\Sale\registration.php

<?php /**
* Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details.
*/
\Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Autosynch_Sale',
__DIR__
);

\app\code\Autosynch\Sale\etc\module.xml

<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Autosynch_Sale" setup_version="0.0.1">
    </module>
</config>

\app\code\Autosynch\Sale\etc\event.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_submit_all_after">
        <observer name="modulename_sales_order_place_after" instance="Autosynch\Sale\Observer\OrderPlaceAfter" />
    </event>
</config>

\app\code\Autosynch\Sale\Observer\OrderPlaceAfter.php

<?php
   namespace Autosynch\Sale\Observer;

    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\Event\Observer;

    class OrderPlaceAfter implements ObserverInterface
    {
        protected $_logger;

        /**
         * @param \Psr\Log\LoggerInterface $_logger
         */

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


        public function execute(Observer $observer)
        {
            $order = $observer->getEvent()->getOrder();

            $this->_logger->info($order->getData());
            $this->_logger->debug($order->getData());

            //var_dump($order->getData());
            //exit;
        }
    }

1 Answer 1

2

You have created to composer.json for your module.

Also, observer definition file name is the wrong event.xml, it should be events.xml

second, to print an array, you should use

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

If you enable developer mode or default mode then might be log print is not working

for log print, you can try to use a custom log file

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

Update Date :- 17th Oct, 2022

for use Logger Method in Magento version: 2.4.3+:

$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/templog.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('Catched event succssfully');
0

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.