Was thinking that we'd need some type of logging mechanism, this is what came out in terms of code of the top of my head:
PHP Code:
<?php
class Logger
{
protected $log = array();
public function addLog(Log $log,$identifier)
{
$this->log[$identifier] = $log;
}
public function log($identifier,$message)
{
$this->log[$identifier]->log($message);
}
}
interface Log
{
public function log($message);
}
class FileLog implements Log
{
protected $file;
protected $handle = null;
public function __construct($file)
{
$this->file = (string)$file;
}
public function __destruct()
{
if($this->handle !== null){
fclose($this->handle);
}
}
public function log($message)
{
$this->openHandle();
fwrite($this->handle,$message,strlen($message));
}
protected function openHandle()
{
if($this->handle === null){
$this->handle = fopen($this->file,'a');
}
}
}
class ExceptionFileLog extends FileLog
{
public function log($e)
{
$message = $e->getMessage();
$code = $e->getCode();
$file = $e->getFile();
$line = $e->getLine();
$trace = $e->getTraceAsString();
$log = "Exception on line: $line, in file: $file.\r\nCode: $code\r\nBacktrace: $trace\r\n";
parent::log($log);
}
}
$logger = new Logger;
$logger->addLog(new FileLog("access.log"),'access');
$logger->addLog(new FileLog("error.log"),'error');
$logger->addLog(new ExceptionFileLog('exception.log'),'exception');
$logger->log('error','Error on line...XXX');
$logger->log('access','Logger.php, 18:09 2005-10-11, from localhost');
try{
throw new Exception("... Error ...");
}catch(Exception $e){
$logger->log('exception',$e);
}
?>
Bookmarks