Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
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.
// 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
}
}