Just seeing what you all think about this class. It is so a programmer could just throw out a command and it would either display an error, or display an error then die(), AND log that error at the same time:
<?php
/**
* @author: Shawn Kurtz
* @email: shawnkurtz[at]gmail.com
* @copyright: 2008
* @project: Error Logging Class
* @filename: index.php
* @edited: 12/2/2008 @ 20:22
*/
class error_logger
{
private $err_file;
private $err_prefix = "<b>Error: </b>";
private $err_suffix = "";
public function __construct($f_err_file)
{
if(!file_exists($f_err_file))
{
$f_create = file_put_contents($f_err_file, "");
if(!f_create)
{
$this->err("Cannnot Create the Error File:". $f_err_file ."!", true);
}
}
$this->err_file = $f_err_file;
}
public function err($f_err_text, $f_log = TRUE)
{
if($f_log)
{
$this->log_err($f_err_text, date('l, \t\h\e jS \of F Y \@ h:i:s A'), $_SERVER['PHP_SELF']);
}
echo $this->err_prefix.$f_err_text.$this->err_suffix;
}
public function err_die($f_err_text, $f_log = TRUE)
{
if($f_log)
{
$this->log_err($f_err_text, date('l, \t\h\e jS \of F Y \@ h:i:s A'), $_SERVER['PHP_SELF']);
}
die($this->err_prefix.$f_err_text.$this->err_suffix);
}
private function log_err($f_err_text, $f_time, $f_page)
{
$f_log_line[] = "Error Time: ". $f_time ."\r\n";
$f_log_line[] = "Error File: ". $f_page ."\r\n";
$f_log_line[] = "Error Text: ". $f_err_text ."\r\n";
$f_log_line[] = "-----\r\n\r\n";
foreach($f_log_line as $f_log_line)
{
$f_write = file_put_contents($this->err_file, $f_log_line, FILE_APPEND);
if(!f_write)
{
$this->err_die("Cannot Write to Error Log: ". $this->err_file ."!", FALSE);
}
}
}
public function clear_log($f_delete = FALSE)
{
if($f_delete)
{
$f_delete = unlink($this->err_file);
if(!$f_delete)
{
$this->err("Could Not Delete Log File!");
}
}
else
{
$f_clear = file_put_contents($this->err_file, "");
if(!f_clear)
{
$this->err("Could Not Clear The Log File!");
}
}
}
}
$error = new error_logger("err.log");
// $error->clear_log();
// $error->clear_log(TRUE);
?>
It would be used something like this:
$errors = new error_logging("errors.log");
if($username !== $real_username)
{
$errors->err($username ." is not a valid user!");
}