3

I usually var_dump($object->getData()); in my module to see what data it contains. Because of the injection dependencies , it looks like I can't do that anymore.

When I try it, nothing is printed out, which is likely because the amount of data connected to it is too large???

Can someone suggest an easy way to debug an object in Magento 2?

Thank you

3

Xdebug is the way to go. You can use Phpstorm in conjunction with Xdebug to pause execution of the code at designated break points and explore ALL data. I use it on Magento 2 every day.

In fact, I made a series of videos on how to install and configure

PhpStorm & Xdebug: Installation & Configuration

I hope this helps to get you going in the right direction!

| improve this answer | |
  • @filne I also recommend to use the xDebug because it is the really best way to obtain all possible object data. – Siarhey Uchukhlebau Sep 10 '16 at 11:24
  • Have you tried xdebug with php7.0...it never breaks and page is permanently loading. – stephen Jan 10 '17 at 14:18
  • While I am not sure why that issue is happening for you, I know that for PHP 7.0 you have to make sure that you have the most recent version of xdebug. In fact, I am using Xdebug on Magento 2 with PHP 7.0 right this minute, no problems. – Shawn Abramson Jan 10 '17 at 14:21
1

You can use this method for log :

$om = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $om->get('Psr\Log\LoggerInterface');
$storeManager->log(100,print_r($priceValue,true));

Here "$priceValue" is the object which you want to print.

| improve this answer | |
  • Where is it getting logged? – Black Sep 21 at 14:09
0

I have found 2 ways of printing an object, either directly in the browser or in a log file in case nothing shows up on your page.

Directly in the browser:

print_r(json_encode($object->getData()));

As in my case using XDebug is not always possible (constraints from how things were setup here before), I created a log file to track my variables' values. Although it's clearly not best practise, but if you don't have a choice this can help out.

Debugging log file function:

function debugging_log($log_msg)
        {
            $log_filename = "{path_to_your_log_file}/debugging.log";
            $log_file_data = '[' . date('Y/m/d H:i:s') . ']' . ' ' . $log_msg;
            file_put_contents($log_filename, $log_file_data . "\n", FILE_APPEND);
        }

Same as for the print_r(), you pass a json object to the function like this:

debugging_log(json_encode($object->getData()));

It prints messages like this in the case of an object:

[2020/10/07 04:45:23] {"id":"5","key_1":"Title 1","key_2":"122","created_at":"2020-10-05 09:34:53"}

Hope this helps!

| improve this answer | |

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.