CLogRouter 中 logroute 是不是需要在调用 collectLogs 或者 processLogs 方法时再初始化
例如:
<?php
class MyLogRouter extends CApplicationComponent
{
private $_routes=array();
private $_routeInitialized = false;
public function init()
{
parent::init();
Yii::getLogger()->attachEventHandler('onFlush',array($this,'collectLogs'));
Yii::app()->attachEventHandler('onEndRequest',array($this,'processLogs'));
}
protected function initRoute()
{
if ( ! $this->_routeInitialized)
{
foreach($this->_routes as $name=>$route)
{
$route=Yii::createComponent($route);
$route->init();
$this->_routes[$name]=$route;
}
$this->_routeInitialized = true;
}
}
public function getRoutes()
{
return new CMap($this->_routes);
}
public function setRoutes($config)
{
foreach($config as $name=>$route)
$this->_routes[$name]=$route;
}
public function collectLogs($event)
{
$this->initRoute();
$logger=Yii::getLogger();
$dumpLogs=isset($event->params['dumpLogs']) && $event->params['dumpLogs'];
foreach($this->_routes as $route)
{
if($route->enabled)
$route->collectLogs($logger,$dumpLogs);
}
}
public function processLogs($event)
{
$this->initRoute();
$logger=Yii::getLogger();
foreach($this->_routes as $route)
{
if($route->enabled)
$route->collectLogs($logger,true);
}
}
}