New and improved error handling class!
Posted: Sun Jul 23, 2006 12:12 pm
EDIT: Keep going down if you want to see the observer examples
EDIT2: This thread will be named back to observer examples later
I spent all morning writing this class, almost 8 straight hours. There is probably alot of work to do on it still but I'm glad to finally have a basic idea.
I tested outputting errors to the browser, and to a log file, both work great!
I would appreciate some feedback on this, anything really, try to keep it constructive and on-topic.
thanks and sorry in advance if its garbage
EDIT2: This thread will be named back to observer examples later
I spent all morning writing this class, almost 8 straight hours. There is probably alot of work to do on it still but I'm glad to finally have a basic idea.
I tested outputting errors to the browser, and to a log file, both work great!
I would appreciate some feedback on this, anything really, try to keep it constructive and on-topic.
thanks and sorry in advance if its garbage
Code: Select all
<?php
class ErrorHandler
{
private $errors;
private $developer_mode;
private $output_mode; // 0 = View, 1 = File, 2 = Db | if ($developer_mode == 1) { $this = 0 }
private $log_file_path;
private $log_file_name;
private $log_file;
private $log_string = '';
private $log_array;
private $log_table;
// Constructor
function __construct($developer_mode, $output_mode, $log_file_path, $log_file_name, $log_table)
{
$this->SetDeveloperMode($developer_mode);
$this->SetOutputMode($output_mode);
$this->SetLogFilePath($log_file_path);
$this->SetLogFileName($log_file_name);
$this->SetLogTable($log_table);
set_error_handler(array($this, 'LogError'));
}
// Setters
function SetDeveloperMode($int)
{
// if $int is an integer and less than 2
if ((is_integer($int) != FALSE) && ($int < 2))
{
$this->developer_mode = $int;
}
// if anything else, kill the script we cannot continue
else
{
die('<b>Fatal Error</b>: Could not construct object, could not set Developer Mode, arguement must be an integer!');
}
}
function SetOutputMode($int) // This always need to be set AFTER $developer_mode
{
// if $int is an integer and less than 2
if ((is_integer($int) != FALSE) && ($int < 2))
{
if (1 == $this->developer_mode)
{
$this->output_mode = 1; // File
}
else
{
$this->output_mode = $int;
}
}
// if anything else, kill the script we cannot continue
else
{
die('<b>Fatal Error</b>: Could not construct object, could not set Output Mode arguement must be an integer!');
}
}
function SetLogFilePath($path)
{
if (is_string($path) != FALSE)
{
if (preg_match('/[^a-zA-Z0-9_.\/]/', $path) == FALSE)
{
$this->log_file_path = $path;
if (substr($path, strlen($path)-1) != '/')
{
$this->log_file_path .= '/';
}
}
else
{
die('<b>Fatal Error</b>: Could not construct object, could not set Log file path, arguement cannot contain any characters that are not foward slash, a-z, A-Z, 0-9, underscore, or period."!');
}
}
else
{
die('<b>Fatal Error</b>: Could not construct object, could not set Log file path, arguement must be a string!');
}
}
function SetLogFileName($filename)
{
if (is_string($filename) != FALSE)
{
if (preg_match('/[^a-zA-Z0-9_.]/', $filename) == FALSE)
{
$this->log_file_name = $filename;
}
else
{
die('<b>Fatal Error</b>: Could not construct object, could not set Log file name, arguement cannot contain any characters that are not a-z, A-Z, 0-9, underscore, or period."!');
}
}
else
{
die('<b>Fatal Error</b>: Could not construct object, could not set Log file name, arguement must be a string!');
}
}
function SetLogTable()
{
// I'm freaking tired I'll do this later, it's just going to check if the table exists, and if not, it will create it.
}
// Getters
function GetDeveloperMode()
{
return $this->developer_mode;
}
function GetOutputMode()
{
return $this->output_mode;
}
function GetLogFilePath()
{
return $this->log_file_path;
}
function GetLogFileName()
{
return $this->log_file_name;
}
function GetLogTable()
{
return $this->log_table;
}
// Error Logging
function LogError($level, $message, $file, $line)
{
$this->StoreError($level, $message, $file, $line);
if (1 == $level)
{
$this->OutputErrorsToView();
die();
}
}
function OutputErrorsToFile()
{
if (file_exists($this->GetLogFilePath()) == FALSE)
{
mkdir($this->GetLogFilePath());
}
$this->log_file = fopen($this->GetLogFilePath().$this->GetLogFileName(), 'a+') or die('could not open file');
foreach ($this->errors as &$layer1)
{
$this->log_string .= implode(',', $layer1);
$this->log_string .= "\r\n";
}
fwrite($this->log_file, $this->log_string);
fclose($this->log_file);
}
function OutputErrorsToDb()
{
foreach ($this->errors as &$layer1)
{
$sql = "INSERT INTO ".$this->GetLogTable()." (id, level, message, file, timestamp, line, class, function)
VALUES (NULL, '%d', '%s', '%s', current_timestamp(), '%d')";
$sql = vprintf($sql, array($layer1[0],
$layer1[1],
$layer1[2],
$layer1[3]));
$link = mysql_connect('localhost', 'root', '****');
mysql_query($sql);
mysql_close($link);
}
}
// Error Viewing
function OutputErrorsFromDb($where = 0, $order = 0)
{
$sql = 'SELECT id, level, message, file, UNIX_TIMESTAMP(timestamp), line, class, function
FROM '.$this->GetLogTable().'
ORDER BY '.$order;
if ((0 != $where) && (is_array($where) != FALSE))
{
$sql .= 'WHERE '.$where[0].' = '.$where[1];
}
if ((0 != $order) && (is_string($order) != FALSE))
{
$sql .= 'ORDER BY '.$order;
}
$link = mysql_connect('localhost', 'root', 'vfr43edc');
$res = mysql_query($sql);
mysql_close($link);
if (mysql_num_rows($result) != FALSE)
{
print "<table>";
while ($row = mysql_fetch_assoc($res))
{
print "<tr>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td>$row[3]</td>
<td>$row[4]</td>
</tr>";
}
print "</table>";
}
else
{
print "There are no errors logged in the database";
}
}
function OutputErrorsFromFile()
{
$this->log_file = fopen($this->GetLogFilePath().$this->GetLogFileName(), 'r');
$this->log_string = fread($this->log_file, 8192);
fclose($this->log_file);
$this->log_string = explode("
", $this->log_string);
foreach ($this->log_string as &$layer1)
{
$this->log_array[] = explode(', ', $layer1);
}
print "<table>";
foreach ($this->log_array as &$layer1)
{
print "<tr>";
foreach ($layer1 as &$layer2)
{
print "<td>$layer2</td>";
}
print "</tr>";
}
print "</table>";
}
function OutputErrorsToView()
{
$i = 1;
foreach ($this->errors as &$layer1)
{
print "<pre>
Error #$i:
Level: $layer1[0]
File: $layer1[1]
Message: $layer1[2]
Line: $layer1[3]
Time: ".date('U', $layer[4])."
</pre>";
$i++;
}
}
// Error Handling
function StoreError($level, $file, $message, $line)
{
$this->errors[count($this->errors)+1] = array($level, $file, $message, $line, date('U'));
}
}
?>