配置下tags.php
<?php
return array(
'view_end'=>array('SlowLog'),
);
可在配置中覆盖慢页面的最大时长和记录读写总数和日志位置<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com>
// +----------------------------------------------------------------------
// $Id: SlowLogBehavior.class.php 2805 2013-02-23 15:15:21Z yangweijie $
/**
+------------------------------------------------------------------------------
* 维护时检测行为扩展 记录数据库查询次数过多或页面显示慢的页面
+------------------------------------------------------------------------------
*/
class SlowLogBehavior extends Behavior {
// 行为参数定义
protected $options = array(
'SLOW_LOG' =>0, //是否记录异常慢的页面
'SLOW_RUN_TIME' => 1, // 最慢运行时间单位s
'SLOW_DB_QUERIES' => 100, // 异常查询次数
'SLOW_LOG_FILE' =>'./slow.log',
);
// 行为扩展的执行入口必须是run
public function run(&$content){
if(C('SLOW_LOG')){
G('beginTime',$GLOBALS['_beginTime']);
G('viewEndTime');
$showTime = G('beginTime','viewEndTime');
$db_count = N('db_query') + N('db_write');
if($showTime >= C('SLOW_RUN_TIME') || $db_count >= C('SLOW_DB_QUERIES')){
$line = date("Y-m-d h:i:s").'|访问了'.__SELF__." {$db_count}次查询,整个页面用了{$showTime}s".PHP_EOL;
file_put_contents(C('SLOW_LOG_FILE'),$line,FILE_APPEND);
}
}
}
}