Cannot Write to Mage Log in Magento 1.8

ce-1.8.1.0logmodule

I'm developing a module on a vanilla install of CE 1.8.1.0. I'm trying to write to a custom log file inside the module's observer. The log file is created but remains empty no matter what. Here's my code:

app/code/local/My/Module/etc/config.xml:

<config>
    <global>
        ...
    </global>
    <frontend>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <my_module>
                        <class>my_module/observer</class>
                        <method>observeSummin</method>
                    </my_module>
                </observers>
            </checkout_cart_product_add_after>
        </events>
    </frontend>
</config>

And the Observer:

app/code/local/My/Module/Model/Observer.php:

class My_Module_Model_Observer
{
    public function observeSummin( $observer ) {
        if ( !$this->_isEnabled ) {
            Mage::log( 'is-enabled, 1: ', 'no', 'my_module.log', true );
            return;
        }

        Mage::log( 'is-enabled, 1: ', 'yes', 'my_module.log', true );

        $quoteItem = $observer->getEvent()->getQuoteItem();

        if ( $this->cartQualifies() ) {
            if ( $quoteItem ) {
                Mage::log( 'cart-qualifies, 1: ', 'yes', 'my_module.log', true );
            }
        } else {
            Mage::log( 'cart-qualifies, 1: ', 'no', 'my_module.log', true );
        }

        return;
    }
}

As you can see, I've just dumped loads of log instances to catch any conditions. Apart from the checkout_cart_product_add_after event, I've also tried hooking into the sales_quote_add_item and checkout_cart_save_after events, with the same results.

If I introduce an intentional code error in the observer, the error log catches it, otherwise, there are none. I've also tried to use Mage::log without the file parameter, but nothing is written to system.log.

The entire var/ folder is chmod 777 and logging is enabled in system config.

I'm lost as to why it's not logging – other Magento events are being logged in system and exception. Any suggestions would be appreciated, thanks!

Best Answer

Perhaps you mean to concatenate the first two arguments (. instead of ,)? In any event, your second argument is being filtered out by the Zend_Log component.

Look at the Mage::log() definition:

public static function log($message, $level = null, $file = '', $forceLog = false)
{
    //snip...
    $level  = is_null($level) ? Zend_Log::DEBUG : $level;
    //snip...
}

According to this use of Zend_Log, your second argument need to be an integer between 0 and 7; best to use the constants though:

class Zend_Log
{
    const EMERG   = 0;  // Emergency: system is unusable
    const ALERT   = 1;  // Alert: action must be taken immediately
    const CRIT    = 2;  // Critical: critical conditions
    const ERR     = 3;  // Error: error conditions
    const WARN    = 4;  // Warning: warning conditions
    const NOTICE  = 5;  // Notice: normal but significant condition
    const INFO    = 6;  // Informational: informational messages
    const DEBUG   = 7;  // Debug: debug messages
    //snip...
}
Related Topic