iFUEL INTERACTIVE

A NY Interactive Agency and a Division of Agency212

See you on Flickr See you on Twitter See you on FaceBook Subscribe

Posts Tagged ‘logging’

Logging all SQL in Magento
October 18th, 2009  |  4 COMMENTS  |  Development, Magento
Tags: , , ,

I’ve been looking for a way to log all the sql that Magento is running for debugging purposes. There are a number of logging mechanisms built in to Magento, but none that would allow you to log all the sql that’s being run. Finally, I’ve found a simple change that can be made to a core file (I know, not ideal because it will get overwritten when you upgrade Magento – but it’s only a few lines in one file).

The Change

1. Open the file <magentoroot>/lib/Varien/Db/Adapter/Pdo/Mysql.php.

2. Add the following lines:

$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);

Add it to the “query” function as shown below:

public function query($sql, $bind = array())
{
     $this->_debugTimer();

     try {
          $sql = (string)$sql;
          if (strpos($sql, ':') !== false || strpos($sql, '?') !== false) {
               $this->_bindParams = $bind;
               $sql = preg_replace_callback('#(([\'"])((\\2)|((.*?[^\\\\])\\2)))#', array($this, 'proccessBindCallback'), $sql);
               $bind = $this->_bindParams;
          }

          $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);

          $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;
 }

Read more

Copyright iFuel Interactive © 2010. All Rights Reserved.
Going up? Click here.A NY INTERACTIVE AGENCY