5

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

4 Answers 4

4

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!

3
  • @filne I also recommend to use the xDebug because it is the really best way to obtain all possible object data. Commented Sep 10, 2016 at 11:24
  • Have you tried xdebug with php7.0...it never breaks and page is permanently loading.
    – tread
    Commented Jan 10, 2017 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. Commented Jan 10, 2017 at 14:21
2

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.

2
  • Where is it getting logged?
    – Black
    Commented Sep 21, 2020 at 14:09
  • @Black did you check var/log/system.log ?
    – Alex
    Commented Jan 13, 2021 at 10:40
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!

0

Most objects have a ->debug() method, which displays less data than ->getData()

This comes in quite handy, when working with n98-magerun2 dev:console

$cm = $di->get(Magento\Customer\Model\Customer::class);
$cm->load(123);
$cm->debug();

# prints the customer's data

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.