CLogger

优质
小牛编辑
128浏览
2023-12-01
所有包 | 属性 | 方法 | 事件
system.logging
继承class CLogger » CComponent
源自1.0
版本$Id: CLogger.php 3515 2011-12-28 12:29:24Z mdomba $
源码framework/logging/CLogger.php
CLogger在内存中记录一条日志信息。

CLogger取回带有不同过滤器条件的日志, 包括日志等级和日志类别。

公共属性

隐藏继承属性

属性类型描述定义在
autoDumpboolean这个属性将被作为参数传递到flush()当它在log()中被调用时, 由于autoFlush已经完成限制。 默认时,这个属性为false,意味着已经过滤的信息仍然保存在内在中, 每次flush()日志路由调用之后。 如果为true,已过滤的信息被保存在实际的媒介中,在每次log()中的flush()被调用之后。CLogger
autoFlushinteger在它们被刷新到目录前多少信息应该被记录。 默认到10,000, 意味着每10,000条信息,这个flush方法自动被发起一次信息。 如果为0,它意味着信息不会被自动刷新。CLogger
executionTimefloat返回服务当前请求的总时间。CLogger
logsarray取回日志信息。CLogger
memoryUsageinteger返回当前应用程序的内存使用量。CLogger
profilingResultsarray返回分析结果。CLogger

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__get()返回一个属性值、一个事件处理程序列表或一个行为名称。CComponent
__isset()检查一个属性是否为null。CComponent
__set()设置一个组件的属性值。CComponent
__unset()设置一个组件的属性为null。CComponent
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
flush()从内存中移除所有记录的信息。CLogger
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getExecutionTime()返回服务当前请求的总时间。CLogger
getLogs()取回日志信息。CLogger
getMemoryUsage()返回当前应用程序的内存使用量。CLogger
getProfilingResults()返回分析结果。CLogger
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasProperty()确定属性是否被定义。CComponent
log()记录一条信息。CLogger
onFlush()发起一个 onFlush 事件。CLogger
raiseEvent()发起一个事件。CComponent

事件

隐藏继承事件

事件描述定义在
onFlush发起一个 onFlush 事件。CLogger

属性详细

autoDump 属性 (可用自 v1.1.8) public boolean $autoDump;

这个属性将被作为参数传递到flush()当它在log()中被调用时, 由于autoFlush已经完成限制。 默认时,这个属性为false,意味着已经过滤的信息仍然保存在内在中, 每次flush()日志路由调用之后。 如果为true,已过滤的信息被保存在实际的媒介中,在每次log()中的flush()被调用之后。

autoFlush 属性 (可用自 v1.1.0) public integer $autoFlush;

在它们被刷新到目录前多少信息应该被记录。 默认到10,000, 意味着每10,000条信息,这个flush方法自动被发起一次信息。 如果为0,它意味着信息不会被自动刷新。

executionTime 属性 只读 public float getExecutionTime()

返回服务当前请求的总时间。 这个方法计算现在和常量YII_BEGIN_TIME定义的时间戳之间的不同 为了估算执行时间更加准确。 此常量应该尽可能早的定义(最好在进入脚本时开始。)

logs 属性 只读 public array getLogs(string $levels='', string $categories='')

取回日志信息。

消息可能会被日志等级and/or类别过滤。 一个等级过滤器通过用逗号或空格分隔的等级列表指定(例如'trace, error')。 一个类别过滤器类似于等级过滤器 (例如'system, system.web')。 一个不同是在等级过滤器中你能使用类似'system.*'的模式去指示所有以 'system'开头的类别。





如果你没有指定等级过滤器,它将取回所有等级的日志。 这同样适用于类别过滤器。

等级过滤器和类别过滤器是可以组合的。 例如,仅仅同时满足两个条件信息才返回。

memoryUsage 属性 只读 public integer getMemoryUsage()

返回当前应用程序的内存使用量。 这个方法依靠PHP的函数memory_get_usage()。 如果它不可用,该方法将尝试使用操作系统程序去确定内存的使用 如果内存使用量仍不能确定将返回0。

profilingResults 属性 只读 public array getProfilingResults(string $token=NULL, string $category=NULL, boolean $refresh=false)

返回分析结果。 此结果可能被令牌and/or类别过滤。 如果没有指定过滤器,返回结果将是每个元素都是数组的数组($token,$category,$time)。

如果指定了一个过滤器,结果将是一个时间数组。

方法详细

flush() 方法 (可用自 v1.1.0)
public void flush(boolean $dumpLogs=false)
$dumpLogsboolean是否处理此日志
源码: framework/logging/CLogger.php#286 (显示) publicfunctionflush($dumpLogs=false)
{
$this->onFlush(newCEvent($this,array('dumpLogs'=>$dumpLogs)));
$this->_logs=array();
$this->_logCount=0;
}

从内存中移除所有记录的信息。 这个方法将引起一个 onFlush 事件。 附加事件处理程序能在它们被移除前处理此日志信息。

getExecutionTime() 方法
public float getExecutionTime()
{return}float服务当前请求的总时间。
源码: framework/logging/CLogger.php#180 (显示) publicfunctiongetExecutionTime()
{
returnmicrotime(true)-YII_BEGIN_TIME;
}

返回服务当前请求的总时间。 这个方法计算现在和常量YII_BEGIN_TIME定义的时间戳之间的不同 为了估算执行时间更加准确。 此常量应该尽可能早的定义(最好在进入脚本时开始。)

getLogs() 方法
public array getLogs(string $levels='', string $categories='')
$levelsstring等级过滤器
$categoriesstring类别过滤器
{return}array信息列表。 每一个数组元素代表一个下面结构的信息 array([0] => message (string)[1] => level (string)[2] => category (string)[3] => timestamp (float, 通过 microtime(true) 取得);
源码: framework/logging/CLogger.php#129 (显示) publicfunctiongetLogs($levels='',$categories='')
{
$this->_levels=preg_split('/[s,]+/',strtolower($levels),-1,PREG_SPLIT_NO_EMPTY);
$this->_categories=preg_split('/[s,]+/',strtolower($categories),-1,PREG_SPLIT_NO_EMPTY);
if(empty($levels)&&empty($categories))
return$this->_logs;
elseif(empty($levels))
returnarray_values(array_filter(array_filter($this->_logs,array($this,'filterByCategory'))));
elseif(empty($categories))
returnarray_values(array_filter(array_filter($this->_logs,array($this,'filterByLevel'))));
else
{
$ret=array_values(array_filter(array_filter($this->_logs,array($this,'filterByLevel'))));
returnarray_values(array_filter(array_filter($ret,array($this,'filterByCategory'))));
}
}

取回日志信息。

消息可能会被日志等级and/or类别过滤。 一个等级过滤器通过用逗号或空格分隔的等级列表指定(例如'trace, error')。 一个类别过滤器类似于等级过滤器 (例如'system, system.web')。 一个不同是在等级过滤器中你能使用类似'system.*'的模式去指示所有以 'system'开头的类别。





如果你没有指定等级过滤器,它将取回所有等级的日志。 这同样适用于类别过滤器。

等级过滤器和类别过滤器是可以组合的。 例如,仅仅同时满足两个条件信息才返回。

getMemoryUsage() 方法
public integer getMemoryUsage()
{return}integer应用程序的内存使用量(用字节)。
源码: framework/logging/CLogger.php#193 (显示) publicfunctiongetMemoryUsage()
{
if(function_exists('memory_get_usage'))
returnmemory_get_usage();
else
{
$output=array();
if(strncmp(PHP_OS,'WIN',3)===0)
{
exec('tasklist/FI"PIDeq'.getmypid().'"/FOLIST',$output);
returnisset($output[5])?preg_replace('/[D]/','',$output[5])*1024:0;
}
else
{
$pid=getmypid();
exec("ps-eo%mem,rss,pid|grep$pid",$output);
$output=explode("",$output[0]);
returnisset($output[1])?$output[1]*1024:0;
}
}
}

返回当前应用程序的内存使用量。 这个方法依靠PHP的函数memory_get_usage()。 如果它不可用,该方法将尝试使用操作系统程序去确定内存的使用 如果内存使用量仍不能确定将返回0。

getProfilingResults() 方法
public array getProfilingResults(string $token=NULL, string $category=NULL, boolean $refresh=false)
$tokenstring令牌过滤器。默认为null,这意味着没有令牌过滤。
$categorystring类别过滤器。默认为null,这意味着没有类别过滤。
$refreshboolean是否刷新内部的时间计算。 如果为false,仅仅第一次调用这个方法进行内部的时间计算。
{return}array分析结果。
源码: framework/logging/CLogger.php#227 (显示) publicfunctiongetProfilingResults($token=null,$category=null,$refresh=false)
{
if($this->_timings===null||$refresh)
$this->calculateTimings();
if($token===null&&$category===null)
return$this->_timings;
$results=array();
foreach($this->_timingsas$timing)
{
if(($category===null||$timing[1]===$category)&&($token===null||$timing[0]===$token))
$results[]=$timing[2];
}
return$results;
}

返回分析结果。 此结果可能被令牌and/or类别过滤。 如果没有指定过滤器,返回结果将是每个元素都是数组的数组($token,$category,$time)。

如果指定了一个过滤器,结果将是一个时间数组。

log() 方法
public void log(string $message, string $level='info', string $category='application')
$messagestring记录的信息
$levelstring信息的等级(例如'Trace', 'Warning', 'Error')。不区分大小写。
$categorystring信息的类别(例如'system.web')。不区分大小写。
源码: framework/logging/CLogger.php#91 (显示) publicfunctionlog($message,$level='info',$category='application')
{
$this->_logs[]=array($message,$level,$category,microtime(true));
$this->_logCount++;
if($this->autoFlush>0&&$this->_logCount>=$this->autoFlush&&!$this->_processing)
{
$this->_processing=true;
$this->flush($this->autoDump);
$this->_processing=false;
}
}

记录一条信息。 通过这个方法记录的消息通过 getLogs 取回。

参见

  • getLogs
onFlush() 方法 (可用自 v1.1.0)
public void onFlush(CEvent $event)
$eventCEvent事件参数
源码: framework/logging/CLogger.php#298 (显示) publicfunctiononFlush($event)
{
$this->raiseEvent('onFlush',$event);
}

发起一个 onFlush 事件。