From 4a2c7c4e9ac75c1420e95624fb9ee34ab178c52f Mon Sep 17 00:00:00 2001 From: xue <> Date: Sun, 8 Jan 2006 07:26:00 +0000 Subject: --- framework/Log/TLogRouter.php | 143 ++++++++++++++++++++++++++++++++++++++----- framework/core.php | 2 +- 2 files changed, 130 insertions(+), 15 deletions(-) diff --git a/framework/Log/TLogRouter.php b/framework/Log/TLogRouter.php index 9060e305..5a3a59dc 100644 --- a/framework/Log/TLogRouter.php +++ b/framework/Log/TLogRouter.php @@ -1,25 +1,47 @@ - + + */ class TLogRouter extends TModule { const CONFIG_FILE_EXT='.xml'; private $_routes=array(); + private $_configFile=null; public function init($config) { - foreach($config->getElementsByName('route') as $routeConfig) + if($this->_configFile!==null) { - $class=$routeConfig->removeAttribute('class'); + if(is_file($this->_configFile)) + { + $dom=new TXmlDocument; + $dom->loadFromFile($this->_configFile); + $this->loadConfig($dom); + } + else + throw new TConfigurationException('logrouter_configfile_invalid',$this->_configFile); + } + $this->loadConfig($xml); + $this->getApplication()->attachEventHandler('EndRequest',array($this,'collectLogs')); + } + + private function loadConfig($xml) + { + foreach($xml->getElementsByTagName('route') as $routeConfig) + { + $properties=$routeConfig->getAttributes(); + if(($class=$properties->remove('class'))===null) + throw new TConfigurationException('logrouter_routeclass_required'); $route=Prado::createComponent($class); - foreach($routeConfig->getAttributes() as $name=>$value) + if(!($route instanceof TLogRoute)) + throw new TConfigurationException('logrouter_routetype_required'); + foreach($properties as $name=>$value) $route->setSubproperty($name,$value); + $this->_routes[]=$route; + $route->init($routeConfig); } - - $this->getApplication()->attachEventHandler('EndRequest',array($this,'collectLogs')); } public function getConfigFile() @@ -56,6 +78,10 @@ abstract class TLogRoute extends TComponent private $_levels=null; private $_categories=null; + public function init($config) + { + } + public function getLevels() { return $this->_levels; @@ -100,22 +126,111 @@ abstract class TLogRoute extends TComponent class TFileLogRoute extends TLogRoute { - const LOG_FILE_EXT='.log'; - private $_file; + private $_maxFileSize=1024; // in KB + private $_maxLogFiles=5; + private $_logPath=null; + private $_fileName='prado.log'; + private $_levelMap=array( + TLogger::ERROR=>'Error', + TLogger::DEBUG=>'Debug', + TLogger::INFO=>'Info', + TLogger::NOTICE=>'Notice', + TLogger::WARNING=>'Warning', + TLogger::ERROR=>'Error', + TLogger::ALERT=>'Alert', + TLogger::FATAL=>'Fatal' + ); + + public function init() + { + if($this->_logPath===null) + throw new TConfigurationException('filelogroute_logfile_required'); + } - public function getLogFile() + public function getLogPath() { - return $this->_file; + return $this->_logPath; } - public function setLogFile($value) + public function setLogPath($value) { - if(($this->_file=Prado::getPathOfNamespace($value,self::LOG_FILE_EXT))===null) - throw new TConfigurationException('filelogroute_logfile_invalid',$value); + if(($this->_logPath=Prado::getPathOfNamespace($value))===null) + throw new TConfigurationException('filelogroute_logpath_invalid',$value); + } + + public function getFileName() + { + return $this->_fileName; + } + + public function setFileName($value) + { + $this->_fileName=$value; + } + + public function getMaxFileSize() + { + return $this->_maxFileSize; + } + + public function setMaxFileSize($value) + { + $this->_maxFileSize=TPropertyValue::ensureInteger($value); + if($this->_maxFileSize<0) + throw new TInvalidDataValueException('filelogroute_maxfilesize_invalid'); + } + + public function getMaxLogFiles() + { + return $this->_maxLogFiles; + } + + public function setMaxLogFiles($value) + { + $this->_maxLogFiles=TPropertyValue::ensureInteger($value); + if($this->_maxLogFiles<1) + throw new TInvalidDataValueException('filelogroute_maxlogfiles_invalid'); } protected function processLogs($logs) { + $str=''; + foreach($logs as $log) + $str.=$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]); + $logFile=$this->_logPath.'/'.$this->_fileName; + if(@filesize($logFile)>$this->_maxFileSize*1024) + $this->rotateFiles(); + $fw=fopen($logFile,'a'); + fwrite($fw,$str); + fclose($fw); + } + + protected function formatLogMessage($message,$level,$category,$time) + { + return @date('M d H:i:s',$time).' ['.$this->getLevelName($level).'] ['.$category.'] '.$message."\n"; + } + + protected function getLevelName($level) + { + return isset(self::$_levelMap[$level])?self::$_levelMap[$level]:'Unknown'; + } + + private function rotateFiles() + { + $file=$this->_logPath.'/'.$this->_fileName; + for($i=$this->_maxLogFiles;$i>0;--$i) + { + $rotateFile=$file.'.'.$i; + if(is_file($rotateFile)) + { + if($i===$this->_maxLogFiles) + unlink($rotateFile); + else + rename($rotateFile,$file.'.'.($i+1)); + } + } + if(is_file($file)) + rename($file,$file.'.1'); } } diff --git a/framework/core.php b/framework/core.php index 3be75010..19f396a3 100644 --- a/framework/core.php +++ b/framework/core.php @@ -723,7 +723,7 @@ class PradoBase return $logger; } - public static function log($msg,$level,$category='Uncategorized') + public static function log($msg,$level=TLogger::INFO,$category='Uncategorized') { if(self::$_logger===null) self::$_logger=new TLogger; -- cgit v1.2.3