Oct 1 2012
Show all queries run by Magento
In this post i will create a simple module that show all the queries run by Magento.
First lets create our directory tree in “app/code/local” as follows:
app/code/local/Simple
app/code/local/Simple/Debug
app/code/local/Simple/Debug/etc
app/code/local/Simple/Debug/Helper
Now, in Simple/Debug/etc, I will create config.xml with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!--app/code/local/Simple/Debug/etc/config.xml--> <?xml version="1.0"?> <config> <modules> <Simple_Debug> <version>1.0.0</version> </Simple_Debug> </modules> <global> <helpers> <simple_debug> <class>Simple_Debug_Helper</class> </simple_debug> </helpers> </global> </config> |
Next we will inform Magento that our module exists by creating a file called Simple_Debug.xml in app/etc/modules:
1 2 3 4 5 6 7 8 9 10 |
<!--app/etc/modules/Simple_Debug.xml--> <?xml version="1.0"?> <config> <modules> <Simple_Debug> <active>true</active> <codePool>local</codePool> </Simple_Debug> </modules> </config> |
Finally I have to create the helper that will log all the queries that Magento run on each request:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
class Simple_Debug_Helper_Data extends Mage_Core_Helper_Abstract { public function logQueries() { $profiler = Mage::getSingleton('core/resource') ->getConnection('core_read') ->getProfiler(); if ($profiler) { $controller = Mage::registry('controller'); $request = $controller->getRequest(); Mage::log('START REQUEST', Zend_Log::INFO, 'queries.log'); Mage::log( 'Route Name:' . $request->getRouteName() . ', Controller Module/Name:' . $request->getControllerModule() . '/'. $request->getControllerName() . ', Action Name:' . $request->getActionName() . ', Request Path:' . $request->getPathInfo(), null, 'queries.log' ); $queries = $profiler->getQueryProfiles(); $counter = 0; foreach ($queries as $query) { if ($query->getQueryType() == Zend_Db_Profiler::SELECT) { Mage::log( number_format($query->getElapsedSecs(),4). ' s '. $query->getQuery(), Zend_Log::INFO, 'queries.log' ); } $counter++; } Mage::log('Queries count:'.$counter, Zend_Log::INFO, 'queries.log'); Mage::log('END REQUEST', Zend_Log::INFO, 'queries.log'); } } } |
You can use this module like this:
1 |
Mage::helper('simple_debug')->logQueries(); |
Just place this code at the end of the index.php.
Enjoy it!
Oct 8 2012
Activate maintenance mode in Magento
Magento has a simple way to turn on maintenance mode. All you have to do is to create maintenance.flagfile in your application root folder. This is need when you want to perform an upgrade to your system or just to put your site offline.
When you are ready with the changes just delete the maintenance.flag file and you are good to go.
Enjoy!
By Iulian Fenici • Magento • 0 • Tags: magento, maintenance mode