<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Logging all SQL in Magento</title>
	<atom:link href="http://blogs.ifuelinteractive.com/2009/10/18/logging-all-sql-in-magento/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.ifuelinteractive.com/2009/10/18/logging-all-sql-in-magento/</link>
	<description>A NY INTERACTIVE AGENCY</description>
	<lastBuildDate>Fri, 21 May 2010 04:45:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: NH</title>
		<link>http://blogs.ifuelinteractive.com/2009/10/18/logging-all-sql-in-magento/comment-page-1/#comment-385</link>
		<dc:creator>NH</dc:creator>
		<pubDate>Thu, 18 Mar 2010 06:41:24 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.ifuelinteractive.com/?p=106#comment-385</guid>
		<description>I guess it still interprets things in pre tags :)

that should be an XML element called &quot;log_verbs&quot; in your DB config.

Unfortunately I also forgot that I modified this piece of code, which puts the log_verbs config into logConfig:


        if (null === $logConfig) {
            // first time function is executed
            // query logger needs to be configured
            if (array_key_exists(&quot;log&quot;, $this-&gt;_config)) {
                $logConfig[&#039;enabled&#039;] = (bool) $this-&gt;_config[&#039;log&#039;];
            }
            if (array_key_exists(&#039;log_file&#039;, $this-&gt;_config)) {
                $logConfig[&#039;file_name&#039;] = $this-&gt;_config[&#039;log_file&#039;];
            }
            if (array_key_exists(&#039;log_verbs&#039;, $this-&gt;_config)) {
                $logConfig[&#039;log_verbs&#039;] = $this-&gt;_config[&#039;log_verbs&#039;];
            } else {
                $logConfig[&#039;log_verbs&#039;] = &quot;SELECT,UPDATE,INSERT,DELETE&quot;;
            }
        }
</description>
		<content:encoded><![CDATA[<p>I guess it still interprets things in pre tags <img src='http://blogs.ifuelinteractive.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>that should be an XML element called &#8220;log_verbs&#8221; in your DB config.</p>
<p>Unfortunately I also forgot that I modified this piece of code, which puts the log_verbs config into logConfig:</p>
<p>        if (null === $logConfig) {<br />
            // first time function is executed<br />
            // query logger needs to be configured<br />
            if (array_key_exists(&#8220;log&#8221;, $this-&gt;_config)) {<br />
                $logConfig['enabled'] = (bool) $this-&gt;_config['log'];<br />
            }<br />
            if (array_key_exists(&#8216;log_file&#8217;, $this-&gt;_config)) {<br />
                $logConfig['file_name'] = $this-&gt;_config['log_file'];<br />
            }<br />
            if (array_key_exists(&#8216;log_verbs&#8217;, $this-&gt;_config)) {<br />
                $logConfig['log_verbs'] = $this-&gt;_config['log_verbs'];<br />
            } else {<br />
                $logConfig['log_verbs'] = &#8220;SELECT,UPDATE,INSERT,DELETE&#8221;;<br />
            }<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: NH</title>
		<link>http://blogs.ifuelinteractive.com/2009/10/18/logging-all-sql-in-magento/comment-page-1/#comment-384</link>
		<dc:creator>NH</dc:creator>
		<pubDate>Thu, 18 Mar 2010 06:38:35 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.ifuelinteractive.com/?p=106#comment-384</guid>
		<description>Thanks, this pointed me in the right direction and provided me with usable code.

I also added a bit to filter out certain SQL verbs. It&#039;s not very efficient, but running in debug mode seldom is :)

* Add to local.xml. Comma-separated list of SQL verbs to log:

SELECT,INSERT


* Above the &quot;//build the log message&quot; in the above code:

        /* Filter SQL verbs */
        if ( strlen( $logConfig[&#039;log_verbs&#039;] ) ) {
            $arr = explode( &quot;,&quot;, $logConfig[&#039;log_verbs&#039;] );

            $words = explode(&#039; &#039;, $sql);
            $verb = $words[0];

            if ( !in_array( $verb, $arr ) ) {
                return $this;
            }
        }

* Add this</description>
		<content:encoded><![CDATA[<p>Thanks, this pointed me in the right direction and provided me with usable code.</p>
<p>I also added a bit to filter out certain SQL verbs. It&#8217;s not very efficient, but running in debug mode seldom is <img src='http://blogs.ifuelinteractive.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>* Add to local.xml. Comma-separated list of SQL verbs to log:</p>
<p>SELECT,INSERT</p>
<p>* Above the &#8220;//build the log message&#8221; in the above code:</p>
<p>        /* Filter SQL verbs */<br />
        if ( strlen( $logConfig['log_verbs'] ) ) {<br />
            $arr = explode( &#8220;,&#8221;, $logConfig['log_verbs'] );</p>
<p>            $words = explode(&#8216; &#8216;, $sql);<br />
            $verb = $words[0];</p>
<p>            if ( !in_array( $verb, $arr ) ) {<br />
                return $this;<br />
            }<br />
        }</p>
<p>* Add this</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fabien Petermann</title>
		<link>http://blogs.ifuelinteractive.com/2009/10/18/logging-all-sql-in-magento/comment-page-1/#comment-49</link>
		<dc:creator>Fabien Petermann</dc:creator>
		<pubDate>Thu, 05 Nov 2009 17:08:54 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.ifuelinteractive.com/?p=106#comment-49</guid>
		<description>Whoops! This blog doesn&#039;t seem to like XML code ;-) So, here&#039;s what should go in your etc/local.xml file (hopefully, this time it will work.. :-)) :

&lt;log&gt;1&lt;/log&gt;
&lt;log_file&gt;&lt;![CDATA[../../../var/debug/sql.txt]]&gt;&lt;/log_file&gt;

e.g. :

&lt;default_setup&gt;
    &lt;connection&gt;
        &lt;host&gt;&lt;![CDATA[localhost]]&gt;&lt;/host&gt;
        &lt;username&gt;&lt;![CDATA[magento]]&gt;&lt;/username&gt;
        &lt;password&gt;&lt;![CDATA[&lt;database_password&gt;]]&gt;&lt;/password&gt;
        &lt;dbname&gt;&lt;![CDATA[&lt;database_name&gt;]]&gt;&lt;/dbname&gt;
        &lt;active&gt;1&lt;/active&gt;
        &lt;log&gt;1&lt;/log&gt;
        &lt;log_file&gt;&lt;![CDATA[/path/to/magento/var/debug/sql.txt]]&gt;&lt;/log_file&gt;
    &lt;/connection&gt;
&lt;/default_setup&gt;</description>
		<content:encoded><![CDATA[<p>Whoops! This blog doesn&#8217;t seem to like XML code <img src='http://blogs.ifuelinteractive.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  So, here&#8217;s what should go in your etc/local.xml file (hopefully, this time it will work.. <img src='http://blogs.ifuelinteractive.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> ) :</p>
<p>&lt;log&gt;1&lt;/log&gt;<br />
&lt;log_file&gt;&lt;![CDATA[../../../var/debug/sql.txt]]&gt;&lt;/log_file&gt;</p>
<p>e.g. :</p>
<p>&lt;default_setup&gt;<br />
    &lt;connection&gt;<br />
        &lt;host&gt;&lt;![CDATA[localhost]]&gt;&lt;/host&gt;<br />
        &lt;username&gt;&lt;![CDATA[magento]]&gt;&lt;/username&gt;<br />
        &lt;password&gt;&lt;![CDATA[&lt;database_password&gt;]]&gt;&lt;/password&gt;<br />
        &lt;dbname&gt;&lt;![CDATA[&lt;database_name&gt;]]&gt;&lt;/dbname&gt;<br />
        &lt;active&gt;1&lt;/active&gt;<br />
        &lt;log&gt;1&lt;/log&gt;<br />
        &lt;log_file&gt;&lt;![CDATA[/path/to/magento/var/debug/sql.txt]]&gt;&lt;/log_file&gt;<br />
    &lt;/connection&gt;<br />
&lt;/default_setup&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fabien Petermann</title>
		<link>http://blogs.ifuelinteractive.com/2009/10/18/logging-all-sql-in-magento/comment-page-1/#comment-48</link>
		<dc:creator>Fabien Petermann</dc:creator>
		<pubDate>Thu, 05 Nov 2009 17:04:32 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.ifuelinteractive.com/?p=106#comment-48</guid>
		<description>Thanks for that post, that gave me a clue as to where I should start off.

I have slightly enhanced your code to get control over query logging through the configuration file. Just replace your code with :

$this-&gt;_logQuery($sql, $bind);

and add this at the bottom of the /path/to/magento/lib/Varien/Db/Adapter/Pdo/Mysql.php file :

protected function _logQuery($sql, $bind = array(), $result = null)
{
    // configuration of the query logger
    static $logConfig = null;

    if (null === $logConfig) {
        // first time function is executed
        // query logger needs to be configured
        if (array_key_exists(&#039;log&#039;, $this-&gt;_config)) {
            $logConfig[&#039;enabled&#039;] = (bool) $this-&gt;_config[&#039;log&#039;];
        }
        if (array_key_exists(&#039;log_file&#039;, $this-&gt;_config)) {
            $logConfig[&#039;file_name&#039;] = $this-&gt;_config[&#039;log_file&#039;];
        }
    }

    if (!$logConfig[&#039;enabled&#039;]) {
        // query logger is not enabled, nothing to do
        return $this;
    }

    // build the log message
    // note: from this part on, code is inspired by _debugStat() and
    // _debugWriteFile() methods of the Varien_Db_Adapter_Pdo_Mysql
    // class, with only small modifications
    $code = &#039;## &#039; . getmypid() . &#039; ## &#039;;
    $nl   = &quot;\n&quot;;

    $code .= &#039;QUERY&#039; . $nl;
    $code .= &#039;SQL: &#039; . $sql . $nl;
    if ($bind) {
        $code .= &#039;BIND: &#039; . print_r($bind, true) . $nl;
    }
    if ($result instanceof Zend_Db_Statement_Pdo) {
        $code .= &#039;AFF: &#039; . $result-&gt;rowCount() . $nl;
    }
    $code .= $nl . $nl;

    if (!array_key_exists(&#039;adapter&#039;, $logConfig)) {
        $logConfig[&#039;adapter&#039;] = new Varien_Io_File();
        $dir = $logConfig[&#039;adapter&#039;]-&gt;dirname($logConfig[&#039;file_name&#039;]);
        $logConfig[&#039;adapter&#039;]-&gt;checkAndCreateFolder($dir);
        $logConfig[&#039;adapter&#039;]-&gt;open(array(&#039;path&#039; =&gt; $dir));
        $logConfig[&#039;file_name&#039;] = basename($logConfig[&#039;file_name&#039;]);
    }

    $logConfig[&#039;adapter&#039;]-&gt;streamOpen($logConfig[&#039;file_name&#039;], &#039;a&#039;);
    $logConfig[&#039;adapter&#039;]-&gt;streamLock();
    $logConfig[&#039;adapter&#039;]-&gt;streamWrite($code);
    $logConfig[&#039;adapter&#039;]-&gt;streamUnlock();
    $logConfig[&#039;adapter&#039;]-&gt;streamClose();

    return $this;
}

Now, edit your /path/to/magento/app/etc/local.xml and add these two lines to your database connection configuration :

1


like in the following :


    
        
        
        &lt;![CDATA[]]&gt;
        &lt;![CDATA[]]&gt;
        1
        1
        
    


Now, all queries can be seen from that sql.txt file. Sweet! :-)

By the way, did you notice this ** HUGE ** security hole: for some reason that I do not understand, on Magento version 1.3.2.3 (maybe this behaviour has changed in newer versions), the default value of Varien_Db_Adapter_Pdo_Mysql::$_debugFile is &#039;var/debug/sql.txt&#039;. If you have multiple stores configured like I do , this file will be located in the DOCUMENT_ROOT of your website (that is /path/to/magento/stores/store1/var/store/sql.txt), hence, making this file ** PUBLIC ** at this URL : http://store1.your-magento-shop.com/var/debug/sql.txt ! 

Knowing this file may contain database credentials (including your database password), I consider this as a very serious threat to the security of your database. Thus, I ** STRONGLY ** recommend to keep this file out of the DOCUMENT_ROOT of the web server by overriding this value, or at least to protect this directory with a .htaccess file.

Yet, you don&#039;t have to worry too much if you only have 1 store configured, as the /path/to/magento_dir/var directory is already protected with a .htacces file.

Thanks again for that code snippet! ;-)

Cheers!</description>
		<content:encoded><![CDATA[<p>Thanks for that post, that gave me a clue as to where I should start off.</p>
<p>I have slightly enhanced your code to get control over query logging through the configuration file. Just replace your code with :</p>
<p>$this-&gt;_logQuery($sql, $bind);</p>
<p>and add this at the bottom of the /path/to/magento/lib/Varien/Db/Adapter/Pdo/Mysql.php file :</p>
<p>protected function _logQuery($sql, $bind = array(), $result = null)<br />
{<br />
    // configuration of the query logger<br />
    static $logConfig = null;</p>
<p>    if (null === $logConfig) {<br />
        // first time function is executed<br />
        // query logger needs to be configured<br />
        if (array_key_exists(&#8216;log&#8217;, $this-&gt;_config)) {<br />
            $logConfig['enabled'] = (bool) $this-&gt;_config['log'];<br />
        }<br />
        if (array_key_exists(&#8216;log_file&#8217;, $this-&gt;_config)) {<br />
            $logConfig['file_name'] = $this-&gt;_config['log_file'];<br />
        }<br />
    }</p>
<p>    if (!$logConfig['enabled']) {<br />
        // query logger is not enabled, nothing to do<br />
        return $this;<br />
    }</p>
<p>    // build the log message<br />
    // note: from this part on, code is inspired by _debugStat() and<br />
    // _debugWriteFile() methods of the Varien_Db_Adapter_Pdo_Mysql<br />
    // class, with only small modifications<br />
    $code = &#8216;## &#8216; . getmypid() . &#8216; ## &#8216;;<br />
    $nl   = &#8220;\n&#8221;;</p>
<p>    $code .= &#8216;QUERY&#8217; . $nl;<br />
    $code .= &#8216;SQL: &#8216; . $sql . $nl;<br />
    if ($bind) {<br />
        $code .= &#8216;BIND: &#8216; . print_r($bind, true) . $nl;<br />
    }<br />
    if ($result instanceof Zend_Db_Statement_Pdo) {<br />
        $code .= &#8216;AFF: &#8216; . $result-&gt;rowCount() . $nl;<br />
    }<br />
    $code .= $nl . $nl;</p>
<p>    if (!array_key_exists(&#8216;adapter&#8217;, $logConfig)) {<br />
        $logConfig['adapter'] = new Varien_Io_File();<br />
        $dir = $logConfig['adapter']-&gt;dirname($logConfig['file_name']);<br />
        $logConfig['adapter']-&gt;checkAndCreateFolder($dir);<br />
        $logConfig['adapter']-&gt;open(array(&#8216;path&#8217; =&gt; $dir));<br />
        $logConfig['file_name'] = basename($logConfig['file_name']);<br />
    }</p>
<p>    $logConfig['adapter']-&gt;streamOpen($logConfig['file_name'], &#8216;a&#8217;);<br />
    $logConfig['adapter']-&gt;streamLock();<br />
    $logConfig['adapter']-&gt;streamWrite($code);<br />
    $logConfig['adapter']-&gt;streamUnlock();<br />
    $logConfig['adapter']-&gt;streamClose();</p>
<p>    return $this;<br />
}</p>
<p>Now, edit your /path/to/magento/app/etc/local.xml and add these two lines to your database connection configuration :</p>
<p>1</p>
<p>like in the following :</p>
<p>        &lt;![CDATA[]]&gt;<br />
        &lt;![CDATA[]]&gt;<br />
        1<br />
        1</p>
<p>Now, all queries can be seen from that sql.txt file. Sweet! <img src='http://blogs.ifuelinteractive.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>By the way, did you notice this ** HUGE ** security hole: for some reason that I do not understand, on Magento version 1.3.2.3 (maybe this behaviour has changed in newer versions), the default value of Varien_Db_Adapter_Pdo_Mysql::$_debugFile is &#8216;var/debug/sql.txt&#8217;. If you have multiple stores configured like I do , this file will be located in the DOCUMENT_ROOT of your website (that is /path/to/magento/stores/store1/var/store/sql.txt), hence, making this file ** PUBLIC ** at this URL : <a href="http://store1.your-magento-shop.com/var/debug/sql.txt" rel="nofollow">http://store1.your-magento-shop.com/var/debug/sql.txt</a> ! </p>
<p>Knowing this file may contain database credentials (including your database password), I consider this as a very serious threat to the security of your database. Thus, I ** STRONGLY ** recommend to keep this file out of the DOCUMENT_ROOT of the web server by overriding this value, or at least to protect this directory with a .htaccess file.</p>
<p>Yet, you don&#8217;t have to worry too much if you only have 1 store configured, as the /path/to/magento_dir/var directory is already protected with a .htacces file.</p>
<p>Thanks again for that code snippet! <img src='http://blogs.ifuelinteractive.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Cheers!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
