From: Jonathan H. <j_...@us...> - 2008-06-08 20:50:55
|
Update of /cvsroot/stack/stack-dev/lib In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv15921/lib Added Files: Logger.php Log Message: Initial commit of logging framework --- NEW FILE: Logger.php --- <?php /** * * Welcome to STACK. A system for teaching and assessment using a * computer algebra kernel. * <br /> * This file is licensed under the GPL License. * <br /> * A copy of the license is in your STACK distribution called * license.txt. If you are missing this file you can obtain * it from: * http://www.stack.bham.ac.uk/license.txt * <br /> * * @author Jonathan Hart (jh...@gm...) * * @package stackUtil */ /** *Logging class for debug messages. *Writes to messages to the $log dir under debug.log *For error logging use the error class. * *Heavily based on Java logging levels and log4j. */ global $config; $root = $config->get('docroot'); require_once $root.'/lib/dateTime.php'; class Logger{ /** * */ private $className; /** * The filename logs will be appended to (in the $log directory) * * @access private * @var $filename */ private $filename = "stack.log"; /** * The current debug level set in the config.php file. * */ private $currentLevel; /** * Debug levels * */ private $debugLevel; /** * Constucts a new logger * * @access public * @param $classname String The name of the class / function the logger is called on. */ public function __construct($className){ $this->className = $className; global $config; switch (strtolower($config->get('debug'))){ case 'false': $this->currentLevel = 0; break; case 'false': $this->currentLevel = 0; break; case 'critical': $this->currentLevel = 10; break; case 'error': $this->currentLevel = 20; break; case 'warn': $this->currentLevel = 30; break; case 'info': $this->currentLevel = 40; break; case 'debug': $this->currentLevel = 50; break; case 'fine': $this->currentLevel = 60; break; case 'finer': $this->currentLevel = 70; break; case 'finest': $this->currentLevel = 80; break; default: $this->currentLevel = 0; } $this->debugLevel['off'] = 0; $this->debugLevel['critical'] = 10; $this->debugLevel['error'] = 20; $this->debugLevel['warn'] = 30; $this->debugLevel['info'] = 40; $this->debugLevel['debug'] = 50; $this->debugLevel['fine'] = 60; $this->debugLevel['finer'] = 70; $this->debugLevel['finest'] = 80; } /** * Logs an info message * * @access public * @param $msg String|Boolean */ public function info($msg){ if($this->currentLevel >= $this->debugLevel['info']){ $this->writeOut("INFO",$msg); } } /** * Logs a debug message * * @access public * @param $msg String|Boolean */ public function debug($msg){ if($this->currentLevel >= $this->debugLevel['debug']){ $this->writeOut("DEBUG",$msg); } } /** * Logs a fine gained debug message * * @access public * @param $msg String|Boolean */ public function fine($msg){ if($this->currentLevel >= $this->debugLevel['fine']){ $this->writeOut("FINE",$msg); } } /** * Logs a finer gained debug message * * @access public * @param $msg String|Boolean */ public function finer($msg){ if($this->currentLevel >= $this->debugLevel['finer']){ $this->writeOut("FINER",$msg); } } /** * Logs a finest gained debug message * * @access public * @param $msg String|Boolean */ public function finest($msg){ if($this->currentLevel >= $this->debugLevel['finest']){ $this->writeOut("FINEST",$msg); } } /** * Logs error that will can be recovered from. * * @access public * @param $msg String|Boolean */ public function warn($msg){ if($this->currentLevel >= $this->debugLevel['warn']){ $this->writeOut("WARN",$msg); } } /** * Logs a error. The system may be able to continue with some data loss * * @access public * @param $msg String|Boolean */ public function error($msg){ if($this->currentLevel >= $this->debugLevel['error']){ $this->writeOut("ERROR",$msg); } } /** * Logs a critical error, the system will be unable to recover from. * * @access public * @param $msg String|Boolean */ public function critical($msg){ if($this->currentLevel >= $this->debugLevel['critical']){ $this->writeOut("CRITICAL",$msg); } } /** * Appends an error to the error log * * @access private * @param $level String * @param $msg String */ private function writeOut($level, $msg){ //2008-12-05 12:34:56 - DEBUG - Logger: Error message global $config; if($config->get('debug')) { //make booleans visible in the logs if(is_bool($msg)){ if($msg){ $msg = "TRUE"; } else{ $msg = "FALSE"; } } $datetime = date('Y-m-d H:i:s'); $message = $datetime.' - '.$level.' - '.$this->className.': '.$msg; $logs = $config->get('logs'); $filename = $logs.$this->filename; if(!file_exists($filename)) { //create file $handle = fopen($filename, 'w'); $head = 'Stack log file. The Logging verbosity is set using the property $this->configArray["debug"] in config.php. See config.php for valid levels. '."\r\n\r\n\r\n\r\n"; fwrite($handle, $head); fclose($handle); } if(is_writable($filename)) { if(!$handle = fopen($filename, 'a')) { echo 'Cannot write Log output, '.$filename.' could not be wrote to.'; } if(fwrite($handle, "$message \r\n\r\n") == false) { echo "Cannot write CAS output to $filename."; } fclose($handle); } else { echo "Cannot write Logs to $filename."; } } } } ?> |