Expanding error handling with a custom handler. All errors are converted to exceptions for easier handling and to help save time because you won't have to type out a bunch of crazy sanity checks, just try/catch FTW.

This commit is contained in:
Josh Sherman 2010-09-19 12:27:55 -04:00
parent 17d5738e77
commit 4307594ab5
4 changed files with 105 additions and 27 deletions

View file

@ -117,27 +117,34 @@ class Log extends Object
private static function write($log_type, $message, $format = true, $time = false)
{
$log_path = LOG_PATH . date('Y/m/d/', ($time == false ? time() : $time));
if (!file_exists($log_path))
try
{
mkdir($log_path, 0777, true);
if (!file_exists($log_path))
{
mkdir($log_path, 0755, true);
}
$log_file = $log_path . $log_type . '.log';
$message .= "\n";
if ($format == true)
{
$backtrace = debug_backtrace();
rsort($backtrace);
$frame = $backtrace[strpos($backtrace[0]['file'], 'index.php') === false ? 0 : 1];
return file_put_contents($log_file, date('H:i:s') . ' ' . str_replace(getcwd(), '', $frame['file']) . ':' . $frame['line'] . ' ' . $message, FILE_APPEND);
}
else
{
return file_put_contents($log_file, $message, FILE_APPEND);
}
}
$log_file = $log_path . $log_type . '.log';
$message .= "\n";
if ($format == true)
catch (ErrorException $exception)
{
$backtrace = debug_backtrace();
rsort($backtrace);
$frame = $backtrace[strpos($backtrace[0]['file'], 'index.php') === false ? 0 : 1];
return file_put_contents($log_file, date('H:i:s') . ' ' . str_replace(getcwd(), '', $frame['file']) . ':' . $frame['line'] . ' ' . $message, FILE_APPEND);
}
else
{
return file_put_contents($log_file, $message, FILE_APPEND);
return false;
}
}
}