Last active
December 29, 2015 10:39
-
-
Save jgraup/7658848 to your computer and use it in GitHub Desktop.
PHP-Error Handling INFO: http://php.net/manual/en/function.set-error-handler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once (__DIR__ . "/../lib/debug/errors.php"); | |
// Test Error | |
echo $doesntExist; | |
// Error Output | |
/* | |
[ERROR (#1) - Tue,26 Nov 2013 - 09:00:00 AM] | |
Message: Undefined variable: doesntExist | |
Error: 8 | |
File: /Your/Path/your/file.php | |
Line: 7 | |
[/ERROR] | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Turn off by using Errors::activate(false); | |
Errors::activate(true); | |
/** | |
* Class Errors | |
* @description Echo error info and store data in static variables when active | |
*/ | |
class Errors | |
{ | |
/** | |
* @var $old_error_handler string | |
* @var $date_zone string | |
* @var $date_format string | |
* @var $date date | |
* @var $errno string | |
* @var $errstr string | |
* @var $errfile string | |
* @var $errline string | |
* @var $inted bool | |
* @var $count int | |
*/ | |
public static $old_error_handler; | |
public static $date_zone = 'America/New_York'; | |
public static $date_format = "D,d M Y - h:m:s A"; | |
public static $date, $count = 0; | |
public static $errno, $errstr, $errfile, $errline; | |
static $inted; | |
/** | |
* @description Activate error info echo | |
* @param bool $isTrue True will echo the error, while false will only save the results | |
*/ | |
public static function activate($isTrue=true) | |
{ | |
if ( !Errors::$inted){ | |
date_default_timezone_set(Errors::$date_zone); | |
Errors::$inted = true; | |
} | |
Errors::$old_error_handler = $isTrue | |
? set_error_handler("Errors::errorHandler") | |
: set_error_handler("Errors::saveErrors"); | |
} | |
/** | |
* @description Stores and echos error info | |
* @param $errno string | |
* @param $errstr string | |
* @param $errfile string | |
* @param $errline string | |
*/ | |
public static function errorHandler($errno, $errstr, $errfile, $errline) | |
{ | |
Errors::saveErrors($errno, $errstr, $errfile, $errline); | |
$date = Errors::$date; | |
$count = Errors::$count; | |
echo "\n\n[ERROR (#{$count}) - {$date}]\n\n\tMessage:\t{$errstr}\n\tError:\t\t{$errno}\n\tFile:\t\t{$errfile}\n\tLine:\t\t{$errline}\n\n[/ERROR]\n\n"; | |
} | |
/** | |
* @description Stores the error info in static variables | |
* @param $errno string | |
* @param $errstr string | |
* @param $errfile string | |
* @param $errline string | |
*/ | |
public static function saveErrors($errno, $errstr, $errfile, $errline) | |
{ | |
Errors::$count++; | |
Errors::$date = date(Errors::$date_format); | |
Errors::$errno = $errno; | |
Errors::$errstr = $errstr; | |
Errors::$errfile = $errfile; | |
Errors::$errline = $errline; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment