【Yii】日志

yii框架中日志组件记录的等级5类,在CLogger已通过常量定义:
const LEVEL_TRACE='trace';
const LEVEL_WARNING='warning';
const LEVEL_ERROR='error';
const LEVEL_INFO='info';
const LEVEL_PROFILE='profile';
CLogger为所有日志写入和获取提供接口,通过日志路由管理类CLogRouter将日志分发到不同日志展现或存储介质中。


日志组件配置

[php]  view plain copy
  1. 'log'=>array(  
  2. 'class'=>'CLogRouter',  
  3. 'routes'=>array(  
  4. array(  
  5. 'class'=>'CFileLogRoute',  
  6. 'levels'=>'error, warning',  
  7. ),  
  8. array(  
  9. 'class'=>'CWebLogRoute',  
  10. 'levels'=>'info',  
  11. 'showInFireBug' => true  
  12. ),               
  13. ),  
  14. ),  

日志路由初始化

在log组件被创建的时候,会通过CLogRouter::init()初始化配置中各个日志路由,并添加刷新和请求结束事件,这个两个事件对YII日志性能提升有很大帮助。YII默认在一次请求中日志的数量小于1000的情况下,日志数据都放在内存中,当大于1000的时候执行onflush事件,将日志刷新到接收介质上,当请求结束的时候执行onEndRequest再刷新一次日志,这样做减少了大量IO操作。

[php]  view plain copy
  1. /** 
  2.  * Initializes this application component. 
  3.  * This method is required by the IApplicationComponent interface. 
  4.  */  
  5. public function init()  
  6. {  
  7.     parent::init();  
  8.     foreach($this->_routes as $name=>$route)  
  9.     {  
  10.         $route=Yii::createComponent($route);  
  11.         $route->init();  
  12.         $this->_routes[$name]=$route;  
  13.     }  
  14.     Yii::getLogger()->attachEventHandler('onFlush',array($this,'collectLogs'));  
  15.     Yii::app()->attachEventHandler('onEndRequest',array($this,'processLogs'));  
  16. }  

日志调用

对日志的操作是通过YII::log()静态方法实现对CLogger的调用,下面是CLogger的log方法,日志会保存在$this->_logs[]中,当触发刷新事件时,执行刷新处理过程。

[php]  view plain copy
  1. /** 
  2.  * Logs a message. 
  3.  * Messages logged by this method may be retrieved back via {@link getLogs}. 
  4.  * @param string $message message to be logged 
  5.  * @param string $level level of the message (e.g. 'Trace', 'Warning', 'Error'). It is case-insensitive. 
  6.  * @param string $category category of the message (e.g. 'system.web'). It is case-insensitive. 
  7.  * @see getLogs 
  8.  */  
  9. public function log($message,$level='info',$category='application')  
  10. {  
  11.       
  12.     $this->_logs[]=array($message,$level,$category,microtime(true));  
  13.     $this->_logCount++;  
  14.     if($this->autoFlush>0 && $this->_logCount>=$this->autoFlush && !$this->_processing)  
  15.     {  
  16.         $this->_processing=true;  
  17.         $this->flush($this->autoDump);  
  18.         $this->_processing=false;  
  19.     }  
  20. }  
以触发onflush事件为例,日志路由CLogRouter::collectLogs($event)将日志对象传给各个日志的collectLogs()中,然后通过processLogs()来处理不同的日志等级对应的展现方式。
[php]  view plain copy
  1. /** 
  2.  * Retrieves filtered log messages from logger for further processing. 
  3.  * @param CLogger $logger logger instance 
  4.  * @param boolean $processLogs whether to process the logs after they are collected from the logger 
  5.  */  
  6. public function collectLogs($logger$processLogs=false)  
  7. {  
  8.     $logs=$logger->getLogs($this->levels,$this->categories);  
  9.     $this->logs=empty($this->logs) ? $logs : array_merge($this->logs,$logs);  
  10.     if($processLogs && !empty($this->logs))  
  11.     {  
  12.         if($this->filter!==null)  
  13.             Yii::createComponent($this->filter)->filter($this->logs);  
  14.         $this->processLogs($this->logs);  
  15.         $this->logs=array();  
  16.     }  
  17. }  

实例

Yii::log("test",CLogger::LEVEL_INFO);
Yii::log("test2",CLogger::LEVEL_INFO);

下面分别是CWebLogRoute在web中和chrome控制台中显示日志的效果。(默认时间和北京时间相差8小时)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值