码迷,mamicode.com
首页 > 其他好文 > 详细

YII框架分析笔记10:日志

时间:2015-09-21 21:18:19      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

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将日志分发到不同日志展现或存储介质中。


日志组件配置

  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操作。

  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[]中,当触发刷新事件时,执行刷新处理过程。

  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()来处理不同的日志等级对应的展现方式。

  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小时)

技术分享


技术分享

YII框架分析笔记10:日志

标签:

原文地址:http://www.cnblogs.com/sunscheung/p/4827131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!