Thursday, 26 February 2015

Logging all the sql in Magento for debug

In Magento development you need some time to check the sql query running in the background

For this you can insert a code into the lib/Varian/Db/Adaptor/Pdo/Mysql.php


$code = 'SQL: ' . $sql . "\r\n";
if ($bind) {
       $code .= 'BIND: ' . print_r($bind, true) . "\r\n";
}
$this->_debugWriteToFile("[".date('Y-m-d H:i:s')."] ".$code);


in your query method of the page.
now your query method look like

    public function query($sql, $bind = array())
    {
        $this->_debugTimer();
        try {
            $this->_checkDdlTransaction($sql);
            $this->_prepareQuery($sql, $bind);
         
/* Inserted code Starts */
           $code = 'SQL: ' . $sql . "\r\n";
           if ($bind) {
                $code .= 'BIND: ' . print_r($bind, true) . "\r\n";
          }
          $this->_debugWriteToFile("[".date('Y-m-d H:i:s')."] ".$code);
/* Inserted code Starts */

            $result = parent::query($sql, $bind);
        } catch (Exception $e) {
            $this->_debugStat(self::DEBUG_QUERY, $sql, $bind);
            $this->_debugException($e);
        }
        $this->_debugStat(self::DEBUG_QUERY, $sql, $bind, $result);
        return $result;
    }


You can find the log at /var/debug/pdo_mysql.log


Mage::log("MyArray:".print_r($arr,true), null, 'logfile.log');

1 comment :

  1. Great article! I think this article will support and complement your point
    Florida Web Development

    ReplyDelete