This might be an odd question. In my shop i have a module which writes a lot to my system.log. The developers told me to ignore it but i like my log to be clean for other things. I'm not allowed to change this module's code by a contract otherwise i could easily put this in a different log.

Is there a way by using the observer perhaps to detect log input from this specific module and intercept it and write it to another log file.

p.s. these messages are always the same and popup hundreds of times, so i could check for certain text in this message to detect and rewrite to another log.

Thanks in advance, im really curious about this one.

  • Are you allowed to modify Mage::log()? If so, use debug_backtrace to get the caller and filter on module path. – Melvyn Mar 14 '15 at 11:39
  • Yes i'm allowed to do that. Thanks for the tip i'll try some things with that. I'll let you know if i made some progress here. – FROSIT Mar 14 '15 at 15:24
up vote 2 down vote accepted

This custom logger is an example that distinguishs between modules: https://github.com/jayelkaake/logmanager

Currently it can only disable logging per module (as by your description, this might be what you want), but you should be able to build upon it to use different files.

For example, adding

$this->_stream = fopen(Mage::getBaseDir('var') . DS . 'log' . DS . $moduleKey '.log');

before

return parent::_write($event);

would log everything to {$moduleName}.log. Downside: no distinction between exception.log, system.log and others anymore.

You actually can, from Mage.php

$writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');

So create a custom module, extend Zend_Log_Writer_Stream and add the next lines to your config.xml

<global>
    <log>
        <core>
            <writer_model>Company_Module_Model_Log_Stream</writer_model>
        </core>
    </log>
</global>

The method ->write($message) is called. You could add your filter here.

You could something like:

/**
 * @var string
 */
protected $_lastMessage;

/**
 * @var int
 */
protected $_messageCounter;

/**
 * Write a message to the log.
 *
 * @param  array  $event  event data
 * @return void
 * @throws Zend_Log_Exception
 */
protected function _write($event)
{
    $line = $this->_formatter->format($event);

    if ($this->_lastMessage == $line) {
        // Same line, do nothing, stop flooding
        $this->_messageCounter++;
        return;
    } else {

        // Check if a notice for repeated message should be added
        if ($this->_messageCounter > 1 && $this->_lastMessage) {
            if (false === @fwrite($this->_stream, sprintf("Last message repeated %s times", $this->_messageCounter))) {
                throw new Zend_Log_Exception("Unable to write to stream");
            }
        }

        // Update counter and last message
        $this->_lastMessage = $line;
        $this->_messageCounter = 1;
    }

    if (false === @fwrite($this->_stream, $line)) {
        #require_once 'Zend/Log/Exception.php';
        throw new Zend_Log_Exception("Unable to write to stream");
    }
}

This way you keep your core code clean.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.