Yii 1.1 Application Development Cookbook 这本书很好
components下面增加log配置,如下:
'components' => array(
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'trace, info, debug, warn, error, fatal, profile',
'categories'=>'test.*',
'maxFileSize'=>1048576,//单文件最大1G
'logFile'=>'test.log',
),
//
// 开发过程中所有日志直接输出到浏览器了,这样不需要登录服务器看日志了
array(
'class' => 'CWebLogRoute',
'categories' => 'test.*',
'levels' => CLogger::LEVEL_PROFILE,
'showInFireBug' => true,
'ignoreAjaxInFireBug' => true,
),
array(
'class' => 'CWebLogRoute',
'categories' => 'test.* ',
),
array(
'class'=>'CEmailLogRoute',
'levels'=>'error, warning',
'emails'=>'admin@example.com',
),
),
),
),
Yii::beginProfile('blockID');
...code block being profiled...
Yii::endProfile('blockID');
Profiling is especially useful when working with database since SQL executions are often the main performance bottleneck of an application. While we can manually insert beginProfile
and endProfile
statements at appropriate places to measure the time spent in each SQL execution, Yii provides a more systematic approach to solve this problem.
By setting CDbConnection::enableProfiling to be true in the application configuration, every SQL statement being executed will be profiled. The results can be readily displayed using the aforementioned CProfileLogRoute, which can show us how much time is spent in executing what SQL statement. We can also call CDbConnection::getStats() to retrieve the total number SQL statements executed and their total execution time.
array(
'class'=>'CProfileLogRoute',
'levels' => CLogger::LEVEL_PROFILE,
'showInFireBug' => true,
'ignoreAjaxInFireBug' => true,
'categories' => 'system.db.* ', //只记录db的操作日志,其他的忽略
),
for($i=0;$i<1000;$i++){
$user = UserModel::model()->findByPk("1");//这里只要是数据库操作就行,这个只是个例子
}
'class'=>'CFileLogRoute',
'levels' => CLogger::LEVEL_PROFILE,
'categories' => 'system.db.* ', //只记录db的操作日志,其他的忽略
'logFile'=>'db.log',
),
$yii = dirname(__FILE__).'/../yii/framework/yii.php';
$config = dirname(__FILE__).'/protected/config/main.php'; defined('YII_DEBUG') or define('YII_DEBUG',true); defined('YII_DEBUG_SHOW_PROFILER') or define('YII_DEBUG_SHOW_PROFILER',true);
//enable profiling
defined('YII_DEBUG_PROFILING') or define('YII_DEBUG_PROFILING',true);
//trace level
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
//execution time
defined('YII_DEBUG_DISPLAY_TIME') or define('YII_DEBUG_DISPLAY_TIME',false);
require_once($yii);
Yii::createWebApplication($config)->run();
2. 在main.php主配置文件里面,的components db 里将enableProfiling设置为true 'components' => array(
'db' => array(
'enableProfiling' => true, //这个是用来记录日志的,会记录每一条语句执行的时间
'enableParamLogging' => true,//true表示包括sql语句的参数在内的信息都会记录到日志里,非常详细
),
)
Yii::log("dfdf",CLogger::LEVEL_INFO,"example");
Yii::log("dfdf",CLogger::LEVEL_ERROR,"example");
Yii::trace("dfdf", "example");
Yii::trace('example trace message', 'example');
Yii::log('info', CLogger::LEVEL_INFO, 'example');
Yii::log('error', CLogger::LEVEL_ERROR, 'example');
Yii::log('trace', CLogger::LEVEL_TRACE, 'example');
Yii::log('warning', CLogger::LEVEL_WARNING, 'example');
Yii::beginProfile('db', 'example');
for($i=0;$i<1000;$i++){
$user = UserModel::model()->findByPk("1");
}
Yii::endProfile('db', 'example');
echo 'done';