php var_dump($object) 或 print_r($object) 到日志文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10385048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:04:11  来源:igfitidea点击:

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

phpmagento

提问by activeDev

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 :

我已经成功地使用 magento 观察者在某些事件发生时调用方法。例如,我正在观察何时通过以下方式保存货件:

<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.

发货 id 打印到日志文件中就好了,但显然它没有按照我想要的方式转储对象,因为我编写的代码是错误的。

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.

非常感谢。

采纳答案by B00MER

If you are in Developer Mode you can try if the object supports debug output via $object->debug().

如果您处于开发人员模式,您可以尝试通过$object->debug().

回答by benmarks

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
);

回答by Jürgen Thelen

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

为了能够使用Mage::log(),需要满足一些条件:

  • developer mode must be set to true
  • logging must be activated in the system configuration.
  • 开发者模式必须设置为 true
  • 必须在系统配置中激活日志记录。

But, you can also forcelogging by passing trueas 4th param to Mage::log().

但是,你也可以强制通过将记录true为第4参数去Mage::log()

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

如果满足所有条件(或强制记录),您应该在var/log/shipping.log.

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

附带说明:Magento 对象往往很大,并且通常包含大量信息,您通常不需要用于日志记录/调试目的。

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

您可以通过使用该getData()方法来减少转储的信息量,该方法是所有 Magento 对象扩展的成员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:

您还可以使用其适当的 getter 方法转储单个属性:

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

If you really need fullobject 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):

如果您确实需要完整的对象转储,我建议使用debug()对象的方法来记录数据(此方法自动检测递归,这有助于避免无限循环占用所有内存):

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_logfunction to log. That's what I sometimes do, if I need just a quick log.

如果您无法开始Mage::log()工作,您也可以使用 PHP 的核心error_log功能进行日志记录。如果我只需要快速记录,这就是我有时会做的事情。

error_log(print_r($shipment->getData(), true), 3, 'shipment.log');

回答by raina77ow

Perhaps you'd use var_export() function instead, like this:

也许你会改用var_export() 函数,像这样:

var_export($shipment, 1); // to return the string without sending it to STDOUT

Also global functions cannot be called directly within double-quoted string. In your case, Mage::log(var_export($shipment, 1) . '------', null, 'shipments.log')would be ok, I guess. )

此外,不能在双引号字符串中直接调用全局函数。在你的情况下, Mage::log(var_export($shipment, 1) . '------', null, 'shipments.log')会好的,我想。)

If the object you're trying to dump contains circular references, var_export() will fail, though. Then you can either use var_dump + ob_start/ob_flush combo (there are several examples at the var_dump() doc page), or take an alternate route with serialize()function.

但是,如果您尝试转储的对象包含循环引用,则 var_export() 将失败。然后,您可以使用 var_dump + ob_start/ob_flush 组合(在var_dump() 文档页面上有几个示例),或者使用serialize()函数采用替代路线。