. */ /** * Provides a logger */ class Logger { private $logfile; private $messages; /** * Constructor * * @param string logfile */ public function __construct($logfile) { $this->logfile = $logfile; $this->messages = array(); } /** * Log an error to file * * @param string error message to be logged */ public function error($msg) { $this->dolog("ERROR", $msg); $this->messages[] = array("Severity"=>"ERROR", "Message"=>$msg); } /** * Log a warning to file * * @param string warning message to be logged */ public function warn($msg) { $this->dolog("WARN", $msg); $this->messages[] = array("Severity"=>"WARN", "Message"=>$msg); } /** * Log an info message to file * * @param string info message to be logged */ public function info($msg) { $this->dolog("INFO", $msg); $this->messages[] = array("Severity"=>"INFO", "Message"=>$msg); } /** * Get the logged messages * * @return array of logged messages. Each entry consists of an array with 'Severity' and 'Message' as key. */ public function getMessages() { return $this->messages; } /** * Log a message * * @param string severity of the message * @param string log message */ private function dolog($severity, $msg) { if( $this->logfile!=NULL ) { if( ($fp = @fopen($this->logfile, "a"))!==false ) { fwrite($fp, date("Y-m-d H:i:s")." ".$severity.": ".$msg." (in ".$_SERVER['REQUEST_URI']." requested from ".$_SERVER['REMOTE_ADDR']." by '".(isset($_SESSION['sess_uid'])?$_SESSION['sess_uid']:"*anonymous*")."')\n"); fclose($fp); }else { $this->messages[] = array("Severity"=>"ERROR", "Message"=>"Failed opening log file: ".$this->logfile); } } } } ?>