Mage::log writes to the screen

I created a really short and sweet reproducible test case for this:

<?php

error_reporting(-1);
ini_set('display_errors', true);
ini_set('memory_limit', '1M');

$chunk = base64_encode(openssl_random_pseudo_bytes(1024));

while (true) {
    $a[] = print_r($chunk, true);
}

The reason you are seeing the information put out is due to print_r using output buffering internally to capture it's information. Take a look at the definition of the print_r function from the PHP source:

/* {{{ proto mixed print_r(mixed var [, bool return])
   Prints out or returns information about the specified variable */
PHP_FUNCTION(print_r)
{
    zval *var;
    zend_bool do_return = 0;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &var, &do_return) == FAILURE) {
        RETURN_FALSE;
    }

    if (do_return) {
        php_start_ob_buffer (NULL, 0, 1 TSRMLS_CC);
    }

    zend_print_zval_r(var, 0 TSRMLS_CC);

    if (do_return) {
        php_ob_get_buffer (return_value TSRMLS_CC);
        php_end_ob_buffer (0, 0 TSRMLS_CC);
    } else {
        RETURN_TRUE;
    }
}

Because PHP is running out of memory and dying, the output buffer is being flushed before print_r clears it out via it's call to php_ob_get_buffer

I'm not sure there is going to be anyway around this. Just make sure you disable that logging in production, or run mod_security to prevent this type of output from going to the page.


What davidalger points out is important. The object you're trying to log is too big and is causing PHP to run out of memory. Depending on your memory limit and the size of your block you might be able to use:

Mage::log($layered_navigation_filter_block->debug());

All objects that extend Varien_Object can use debug() to output the underlying _data property recursively.

Check out this blog post by one of my co-workers for a more detailed explanation.


could this be the reason?

https://stackoverflow.com/questions/9329877/using-print-r-in-ob-start

"From the PHP documentation: When the return parameter is used, this function uses internal output buffering so it can not be used inside an ob_start() callback function." More info here: [php.net/manual/en/function.print-r.php]

Tags:

Debugging