1

I am trying to observe an event. How can I debug or know if the observer function has been called? I am using the event "checkout_cart_add_product_complete" and trying to do something simple when a product is added to the cart. But nothing happen when I add the product. I would like to know if the function was reached. My "events.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_cart_add_product_complete">
    <observer name="send_order_tracy" instance="Tracy\ShippingMethod\Observer\SendOrderToTracy"/>
</event>
</config>

My observer class:

<?php

namespace Tracy\ShippingMethod\Observer;

class SendOrderToTracy implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        debug_to_console("por la chuchaaa");
        //die('Observer Is called!');
    }

    function debug_to_console( $data ) {
        $output = $data;
        if ( is_array( $output ) )
            $output = implode( ',', $output);

        echo "<script>console.log( 'Debug Objects: " . $output . "' );</script>";
    }
}
1

You can use below code in your observer execute function like this-

public function execute(\Magento\Framework\Event\Observer $observer)
    {
       $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/test.log'); // creates new log file named test.log in var/log folder
       $logger = new \Zend\Log\Logger();
       $logger->addWriter($writer);
       $logger->info("observer called"); // this will be print in test.log file

    }

it will create new log file named 'test.log' in var/log folder and it will print string 'observer called' in it if observer is called.

  • Thanks @Shashank, I will try this. By the way, with any change is necessary to compile using: "php bin/magento setup:di:compile"? because this process take a lot of minutes in my enviroment. – – Daniel Silva Oct 16 '18 at 18:41
  • 1
    not for all changes in observer..just need to clear cache .. – Shashank Kumrawat Oct 16 '18 at 18:43
  • @ShashankKumrawat For Magento 1 how can i do that? – zus Aug 20 at 9:28
  • 1
    @zus, you can use Mage::log('text', null, "log.log", true); in magento1 – Shashank Kumrawat Aug 21 at 7:15
  • How to do what event trigger if observer executed and print triggered event in log file – zus Aug 21 at 7:19
1

try to add this script on your debug_to_console function :

\Magento\Framework\App\ObjectManager::getInstance()
    ->get(\Psr\Log\LoggerInterface::class)->debug('observer function called');

it will write message to your log file, I don't remember what is exactly but you can find in these log file debug.log, system.log or exception.log just check one of them.

note : objectManager is not recommended way, but I write this just to debug and make sure your observer called. After you found it works just deleted this code part

  • Thanks @mrfizh, I will try this. By the way, with any change is necessary to compile using: "php bin/magento setup:di:compile"? because this process take a lot of minutes in my enviroment. – Daniel Silva Oct 16 '18 at 18:29
  • 1
    I think you don't need to do di:compile, it should be just executed since you already do di:compile after define the observer at the first time – mrfizh Oct 16 '18 at 18:33

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.