0

I have two question regarding Mage::Log.

We are working on an application using Magento. We would like to write programming errors to custom database table. Is it possible to do the same using Mage::Log.

Is it recommended to write the error log to a database table. Or should we continue using the exception.log and system.log. We have added some try catch block in our code, so if there are any exceptions occurred, then would like write it to a DB table or to log files. Which is the best practice to follow.

2 Answers 2

0

Mage::log() is defined in app/Mage.php

If you want to write to a db when you do a Mage::log('somestuff') you've to modify the file Mage.php in something like this:

public static function log($message, $level = null, $file = '', $forceLog = false)
    {
        if (!self::getConfig()) {
            return;
        }

        try {
            $logActive = self::getStoreConfig('dev/log/active');
            if (empty($file)) {
                $file = self::getStoreConfig('dev/log/file');
            }
        }
        catch (Exception $e) {
            $logActive = true;
        }

        if (!self::$_isDeveloperMode && !$logActive && !$forceLog) {
            return;
        }

        static $loggers = array();

        $level  = is_null($level) ? Zend_Log::DEBUG : $level;
        $file = empty($file) ? 'system.log' : $file;

        try {
            if (!isset($loggers[$file])) {
                $logDir  = self::getBaseDir('var') . DS . 'log';
                $logFile = $logDir . DS . $file;

                if (!is_dir($logDir)) {
                    mkdir($logDir);
                    chmod($logDir, 0750);
                }

                if (!file_exists($logFile)) {
                    file_put_contents($logFile, '');
                    chmod($logFile, 0640);
                }

                $format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
                $formatter = new Zend_Log_Formatter_Simple($format);
                $writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');
                if (!self::$_app || !$writerModel) {
                    $writer = new Zend_Log_Writer_Stream($logFile);
                }
                else {
                    $writer = new $writerModel($logFile);
                }
                $writer->setFormatter($formatter);
                $loggers[$file] = new Zend_Log($writer);
            }

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

            $loggers[$file]->log($message, $level);
            // START 
            $write = Mage::getSingleton("core/resource")->getConnection("core_write");
            $query = "INSERT INTO  your_log_table (message, level, logfile) values (:message, :level, :logfile)";
            $binds = array( 'message'    => $message, 'level'   => $level, 'logfile'    => $logFile);
            $write->query($query, $binds);
            // END

        }
        catch (Exception $e) {
        }
    }
2
  • Is ok to edit the Mage.php file? Will it overwritten while upgrading Magento? Feb 2, 2016 at 11:46
  • is never ok to edit a core file. Feb 2, 2016 at 17:19
0

IMHO the line here from the Mage::log() method

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

shows how to register a own log writer, through a node in the local.xml.

<config>        
   <global>
       <log>
           <core>
               <writer_model>Zend_Log_Writer_Db</writer_model>
           </core>
       </log>
   </global>
<config>

You can extend the Zend_Log_Writer_Db and create your DB connection, table and columnmapping in the construtor, beacause Mage::log() just create a new instance here.

if (!self::$_app || !$writerModel) {
    $writer = new Zend_Log_Writer_Stream($logFile);
} else {
    $writer = new $writerModel($logFile);
}

So INHO no Core changes need or for DB logging or recommended ;)

But please correct me if I am wrong!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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