如何将数组内容打印到Magento CE 1.7中的日志文件中,而无需通过循环进行迭代?

有帮助吗?

解决方案

Mage::log(print_r($arr, 1), null, 'logfile.log');

将第二个参数添加到 print_r 将返回带有打印变量的字符串。
编辑
根据下面的评论,我有义务提供记录阵列的其他选项。

Mage::log($arr, null, 'logfile.log');

或者如果您需要数组的字符串前缀

Mage::log('prefix'.Zend_Debug::dump($arr, null, false), null, 'logfile.log');

第二个参数 Zend_Debug::dump() 是标签。如果不是 null 它将在数组转储之前添加到。
第三个参数 Zend_Debug::dump() 方法 echo. 。如果它是 true 然后,如果是 false 它将作为字符串返回。在您的情况下,您需要它 false.

其他提示

您是否只是尝试这样做:

Mage::log($array, null, 'logfile.log', true);

法师日志方法应扩展自我。

正如Petar指出的那样,它已经扩展,因此,如果它是数组或对象,则不需要print_r。但是,如果您将其混合在一起,例如:

Mage::log('my string' . $array);

您会遇到问题,因为phps数组到字符串转换的意思是:

array(... whatever...) -> 'String'

使用对象,PHP试图调用__Tostring方法如果不存在,则会丢弃错误(我认为)。

并参考 Mage::log():

\Mage::log
/app/Mage.php:784
public static function log($message, $level = null, $file = '', $forceLog = false)
{
    // ...
    // initialize ... blah stuff...
    // check wether logging is on, developer mode or logging is forced

    try {
        // get the file, define the format... more stuff ... blah ...

        if (is_array($message) || is_object($message)) {
            $message = print_r($message, true);
        }

        $loggers[$file]->log($message, $level);
    }
    catch (Exception $e) {
    }
}

什么都没有测试:-)

许可以下: CC-BY-SA归因
scroll top