You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
During development, it's often very handy to have SQL logging and profiling.
I found that this was tricky in Yii 1.0, but with Yii 1.1, I'm pleased to say, this is now much easier. While the new CWebLogRoute, by default, will inject it's output after the </html>
tag, producing invalid markup and often resulting in strange rendering artifacts, logging to the FireBug console is a very useful feature!
Personally, I prefer to build this into my "index.php" file - here's an example of how to modify the configuration depending on the domain name, to have the SQL logging and profiling turned on when you're testing your site from a local domain.
Example "index.php" file:
<?php
// change the following paths if necessary
$yii=dirname(dirname(__FILE__)).'/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
// remove the following line when in production mode
if ($_SERVER['HTTP_HOST'] == 'localhost') {
defined('YII_DEBUG') or define('YII_DEBUG',true);
$config['components']['log']['routes'][] = array(
'class'=>'CWebLogRoute',
'categories'=>'system.db.CDbCommand',
'showInFireBug'=>true,
);
$config['components']['db']['enableProfiling'] = true;
$config['components']['db']['enableParamLogging'] = true;
}
require_once($yii);
Yii::createWebApplication($config)->run();
It's as simple as that - now run the site with FireBug enabled, and you should see your SQL queries, with parameters, displayed on the Console tab in FireBug!
If it does not work from index.php
Configure it in your config file:
'components'=>array( 'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( 'class'=>'CFileLogRoute', 'levels'=>'error, warning', ), array( 'class'=>'CWebLogRoute', 'categories'=>'system.db.CDbCommand', 'showInFireBug'=>true, ), // uncomment the following to show log messages on web pages /* array( 'class'=>'CWebLogRoute', ), */ ), ), 'db'=>array( 'enableProfiling'=>true, 'enableParamLogging'=>true, ), ),
Missing require()
This will work from index.php
... $config=require(dirname(__FILE__).'/protected/config/main.php'); ...
enable logging on ajax requests
Can we log database queries on ajax requests too?
I am logging alright but how can I enable logging database queries on ajax requests?
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.