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.
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.
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.
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.
@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 .
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
);
To be able to use Mage::log()
, some conditions need to be met:
true
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With