Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php var_dump($object) or print_r($object) to a log file

Tags:

php

magento

This question is generic, and I would simply like to know how to dump objects to log files. In order to clarify things, i am elaborating through an example.

I have been successfully using magento observers to call methods when certain events happen. As an example, I am observing for when a shipment is saved via :

<sales_order_shipment_save_after>

and i'm successfully calling a method. I would like to grab the shipment and simply dump the object to a log file. eg.

public function newShipment(Varien_Event_Observer $observer)
{
    $shipment = $observer->getEvent()->getShipment();
    $shipId = $shipment->getId();
    Mage::log("shipment ({$shipId}) created/saved", null, 'shipments.log');
    //trying to dump $shipment data into the log file
    Mage::log("({var_dump($shipment)}) ------", null, 'shipments.log');
    Mage::log("----------------------------", null, 'shipments.log');
}

The shipment id is printed into the log file just fine, but obviously it does not dump the object the way i want it to as the code I have written is wrong.

Can anyone tell me how I could dump an object to a log file and perhaps give me some advice on logging in general?

thanks very much.

like image 738
activeDev Avatar asked Apr 30 '12 14:04

activeDev


People also ask

What is the difference between Var_dump () and Print_r ()?

var_dump() displays values along with data types as output. print_r() displays only value as output. It does not have any return type. It will return a value that is in string format.

Why Var_dump () is preferable over Print_r ()?

It's too simple. The var_dump() function displays structured information about variables/expressions including its type and value. Whereas The print_r() displays information about a variable in a way that's readable by humans. Example: Say we have got the following array and we want to display its contents.

What is the use of Print_r in PHP?

It is a built-in function in print_r in PHP that is used to print or display the contents of a variable. It essentially prints human-readable data about a variable. The value of the variable will be printed if it is a string, integer, or float.

What does Var_dump return?

@JMTyler var_export returns a parsable string—essentially PHP code—while var_dump provides a raw dump of the data. So, for example, if you call var_dump on an integer with the value of 1, it would print int(1) while var_export just prints out 1 .


2 Answers

Mage::log(
    $object->debug(), //Objects extending Varien_Object can use this
    Zend_Log::DEBUG,  //Log level
    'my.log',         //Log file name; if blank, will use config value (system.log by default)
    true              //force logging regardless of config setting
);
like image 50
benmarks Avatar answered Sep 20 '22 14:09

benmarks


To be able to use Mage::log(), some conditions need to be met:

  • developer mode must be set to true
  • logging must be activated in the system configuration.

But, you can also force logging by passing true as 4th param to Mage::log().

If all conditions are met (or logging is forced), you should find your log file in var/log/shipping.log.

As a side note: Magento objects tend to be huge and usually contain tons of informations that you usually don't really need for logging/debugging purposes.

You can reduce the amount of dumped information by using the getData() method, a member of all Magento objects extending Varien_Object:

Mage::log(print_r($shipment->getData(), true), null, 'shipment.log', true);

You can also dump single attributes by using its proper getter method:

Mage::log((string) $shipment->getId(), null, 'shipment.log', true);

If you really need full object dumps, I'd recommend to use the debug() method of the object to log the data (this method autodetects recursions which helps avoiding endless loops eating up all memory):

Mage::log($shipment->debug(), null, 'shipment.log', true);

If you can't get Mage::log() to work, you alternatively could use PHP's core error_log function to log. That's what I sometimes do, if I need just a quick log.

error_log(print_r($shipment->getData(), true), 3, 'shipment.log');
like image 40
Jürgen Thelen Avatar answered Sep 17 '22 14:09

Jürgen Thelen