dd

CProfileLogRoute


system.logging
继承 class CProfileLogRoute » CWebLogRoute » CLogRoute » CComponent
源自 1.0
版本 $Id: CProfileLogRoute.php 3515 2011-12-28 12:29:24Z mdomba $
源码
CProfileLogRoute在网页中显示分析结果。

通过调用YiiBase::beginProfile()YiiBase::endProfile()完成分析, 它们标记开始和结束的代码块。

CProfileLogRoute通过设置report属性支持两种类型报告:
  • summary: 每一个标记代码块的执行时间列表
  • callstack: 在一个分层的视图中列出标记代码块,反映它们的调用顺序。

公共属性

属性 类型 描述 定义在
categories string 被逗号或空格分隔的类别列表。默认为空,意味着所有类别。 CLogRoute
enabled boolean 是否启用这个日志路由。默认为true。 CLogRoute
filter mixed 附加过滤器 (例如 CLogFilter) 它被应用到日志信息。 这个属性的值被传递到 Yii::createComponent 创建一个日志过滤器对象。 结果,这可能是一个表示过滤器类名的字符串或一个表示过滤器配置的数组。

总之,日志过滤器类应该是 CLogFilter 或它的一个子类。 默认为null,意味着没有过滤器被使用。
CLogRoute
groupByToken boolean 是否按照分析令牌总计结果。 如果为false,结果将被类别合并。 默认为true。注意这个属性仅仅对摘要报告有效, 当report是‘summary’时启用它。 CProfileLogRoute
ignoreAjaxInFireBug boolean 在Firebug中的Ajax调用的日志是否应该被忽略。默认为true。 这个选项要小心使用,,因为一个ajax调用返回所有的输出作为一个结果数据。 例如,如果ajax调用一个json格式的返回结果,任何来自日志记录器的输出将引起ajax调用失败。 CWebLogRoute
levels string 用逗号或空格分隔的等级列表。默认是空,意味着所有等级。 CLogRoute
logs array 到目前为止这个日志路由搜集的日志。 CLogRoute
report string 要显示的分析报告的类型。默认为‘summary’。 CProfileLogRoute
showInFireBug boolean 是否此日期应该显示在FireBug而不是浏览器窗口。默认为false。 CWebLogRoute

公共方法

方法 描述 定义在
__call() 如果类中没有调的方法名,则调用这个方法。 CComponent
__get() 返回一个属性值、一个事件处理程序列表或一个行为名称。 CComponent
__isset() 检查一个属性是否为null。 CComponent
__set() 设置一个组件的属性值。 CComponent
__unset() 设置一个组件的属性为null。 CComponent
asa() 返回这个名字的行为对象。 CComponent
attachBehavior() 附加一个行为到组件。 CComponent
attachBehaviors() 附加一个行为列表到组件。 CComponent
attachEventHandler() 为事件附加一个事件处理程序。 CComponent
canGetProperty() 确定属性是否可读。 CComponent
canSetProperty() 确定属性是否可写。 CComponent
collectLogs() 从日志记录器取回已过滤的日志信息以便进一步处理。 CLogRoute
detachBehavior() 从组件中分离一个行为。 CComponent
detachBehaviors() 从组件中分离所有行为。 CComponent
detachEventHandler() 分离一个存在的事件处理程序。 CComponent
disableBehavior() 禁用一个附加行为。 CComponent
disableBehaviors() 禁用组件附加的所有行为。 CComponent
enableBehavior() 启用一个附加行为。 CComponent
enableBehaviors() 启用组件附加的所有行为。 CComponent
evaluateExpression() 计算一个PHP表达式,或根据组件上下文执行回调。 CComponent
getEventHandlers() 返回一个事件的附加处理程序列表。 CComponent
getReport() 返回要显示的分析报告的类型。默认为‘summary’。 CProfileLogRoute
hasEvent() 确定一个事件是否定义。 CComponent
hasEventHandler() 检查事件是否有附加的处理程序。 CComponent
hasProperty() 确定属性是否被定义。 CComponent
init() 初始化此路由。 CProfileLogRoute
processLogs() 显示日志信息。 CProfileLogRoute
raiseEvent() 发起一个事件。 CComponent
setReport() 设置要显示的分析报告的类型。有效值包括‘summary’和‘callstack’。 CProfileLogRoute

受保护方法

方法 描述 定义在
aggregateResult() 聚焦报告结果。 CProfileLogRoute
displayCallstack() 显示分析程序为显示的调用堆栈。 CProfileLogRoute
displaySummary() 显示分析结果的报告摘要。 CProfileLogRoute
formatLogMessage() 格式化一条日志信息已给定不同字段。 CLogRoute
render() 渲染该视图。 CWebLogRoute

属性详细

groupByToken 属性
public boolean $groupByToken;

是否按照分析令牌总计结果。 如果为false,结果将被类别合并。 默认为true。注意这个属性仅仅对摘要报告有效, 当report是‘summary’时启用它。

report 属性
public string getReport()
public void setReport(string $value)

要显示的分析报告的类型。默认为‘summary’。

方法详细

aggregateResult() 方法
protected array aggregateResult(array $result, float $delta)
$result array 这个代码块的日志结果
$delta float 这个代码块花费的时间
{return} array
protected function aggregateResult($result,$delta)
{
    list(
$token,$calls,$min,$max,$total)=$result;
    if(
$delta<$min)
        
$min=$delta;
    else if(
$delta>$max)
        
$max=$delta;
    
$calls++;
    
$total+=$delta;
    return array(
$token,$calls,$min,$max,$total);
}

聚焦报告结果。

displayCallstack() 方法
protected void displayCallstack(array $logs)
$logs array 日志列表
protected function displayCallstack($logs)
{
    
$stack=array();
    
$results=array();
    
$n=0;
    foreach(
$logs as $log)
    {
        if(
$log[1]!==CLogger::LEVEL_PROFILE)
            continue;
        
$message=$log[0];
        if(!
strncasecmp($message,'begin:',6))
        {
            
$log[0]=substr($message,6);
            
$log[4]=$n;
            
$stack[]=$log;
            
$n++;
        }
        else if(!
strncasecmp($message,'end:',4))
        {
            
$token=substr($message,4);
            if((
$last=array_pop($stack))!==null && $last[0]===$token)
            {
                
$delta=$log[3]-$last[3];
                
$results[$last[4]]=array($token,$delta,count($stack));
            }
            else
                throw new 
CException(Yii::t('yii','CProfileLogRoute found a mismatching code block "{token}". Make sure the calls to Yii::beginProfile() and Yii::endProfile() be properly nested.',
                    array(
'{token}'=>$token)));
        }
    }
    
// remaining entries should be closed here
    
$now=microtime(true);
    while((
$last=array_pop($stack))!==null)
        
$results[$last[4]]=array($last[0],$now-$last[3],count($stack));
    
ksort($results);
    
$this->render('profile-callstack',$results);
}

显示分析程序为显示的调用堆栈。

displaySummary() 方法
protected void displaySummary(array $logs)
$logs array 日志列表
protected function displaySummary($logs)
{
    
$stack=array();
    foreach(
$logs as $log)
    {
        if(
$log[1]!==CLogger::LEVEL_PROFILE)
            continue;
        
$message=$log[0];
        if(!
strncasecmp($message,'begin:',6))
        {
            
$log[0]=substr($message,6);
            
$stack[]=$log;
        }
        else if(!
strncasecmp($message,'end:',4))
        {
            
$token=substr($message,4);
            if((
$last=array_pop($stack))!==null && $last[0]===$token)
            {
                
$delta=$log[3]-$last[3];
                if(!
$this->groupByToken)
                    
$token=$log[2];
                if(isset(
$results[$token]))
                    
$results[$token]=$this->aggregateResult($results[$token],$delta);
                else
                    
$results[$token]=array($token,1,$delta,$delta,$delta);
            }
            else
                throw new 
CException(Yii::t('yii','CProfileLogRoute found a mismatching code block "{token}". Make sure the calls to Yii::beginProfile() and Yii::endProfile() be properly nested.',
                    array(
'{token}'=>$token)));
        }
    }

    
$now=microtime(true);
    while((
$last=array_pop($stack))!==null)
    {
        
$delta=$now-$last[3];
        
$token=$this->groupByToken $last[0] : $last[2];
        if(isset(
$results[$token]))
            
$results[$token]=$this->aggregateResult($results[$token],$delta);
        else
            
$results[$token]=array($token,1,$delta,$delta,$delta);
    }

    
$entries=array_values($results);
    
$func=create_function('$a,$b','return $a[4]<$b[4]?1:0;');
    
usort($entries,$func);

    
$this->render('profile-summary',$entries);
}

显示分析结果的报告摘要。

getReport() 方法
public string getReport()
{return} string 要显示的分析报告的类型。默认为‘summary’。
public function getReport()
{
    return 
$this->_report;
}

init() 方法
public void init()
public function init()
{
    
$this->levels=CLogger::LEVEL_PROFILE;
}

初始化此路由。 这个方法在路由管理器创建此路由后发起。

processLogs() 方法
public void processLogs(array $logs)
$logs array 日志信息列表
public function processLogs($logs)
{
    
$app=Yii::app();
    if(!(
$app instanceof CWebApplication) || $app->getRequest()->getIsAjaxRequest())
        return;

    if(
$this->getReport()==='summary')
        
$this->displaySummary($logs);
    else
        
$this->displayCallstack($logs);
}

显示日志信息。

setReport() 方法
public void setReport(string $value)
$value string 要显示的分析报告的类型。有效值包括‘summary’和‘callstack’。
public function setReport($value)
{
    if(
$value==='summary' || $value==='callstack')
        
$this->_report=$value;
    else
        throw new 
CException(Yii::t('yii','CProfileLogRoute.report "{report}" is invalid. Valid values include "summary" and "callstack".',
            array(
'{report}'=>$value)));
}