Error Logging Class
Posted: Thu Jul 27, 2006 4:32 am
I've been putting off building an error log class for a while, but I really did need one so I wrote this. It's very simple but it's all I really need. Maybe this can go in snippets.
Call with..
Code: Select all
// Sample windows path c:\\directory\\error_log.txt
class errorLogger
{
function __construct($errorLog = '/server/projects/error_log.txt')
{
$this->log = @fopen($errorLog, 'a+b'); // open log
if ((is_resource($this->log)) && (is_writable($errorLog)))
{
$this->ready = true;
} else {
$this->ready = false; // could not open file
trigger_error("<b>The file " . $errorLog . " could not be opened or is not writable.</b>", E_USER_NOTICE);
}
}
public function newEntry($entry)
{
if ($this->ready) fwrite($this->log, $entry . "\n");
}
public function clear()
{
if ($this->ready) ftruncate($this->log, 0);
}
function __destruct()
{
if ($this->ready) fclose($this->log); // close the file
}
}Code: Select all
$errorLog = new errorLogger();
$errorLog->newEntry("This is test error"); // add entry
$errorLog->clear(); // delete log