ViaThinkSoft CodeLib
This article is in:
CodeLib → Programming aids → PHP
function myErrorHandler( $type, $msg, $file=null, $line=null, $args=null )
{
// Original source: http://www.tutorials.de/blog/gumbos-blog-11282/funktion-zur-fehlermeldung-und-behandlung-521/
// Modificated by Daniel Marschall:
// - Allow the use of trigger_error() instead of the direct use of the error handler function.
// - Added E_USER_DEPRECATED as well as more compatibility parts
// - Respect the current 'html_errors' configuration
// - Respect '$php_errormsg' when 'track_errors' is enabled
// Problems:
// - The @ sign is not respected when trigger_error() is called :-(
$backtrace = debug_backtrace();
$i = count($backtrace) > 1 ? 1 : 0;
if (is_null($file)) {
$file = $backtrace[$i]['file'];
}
if (is_null($line)) {
$line = $backtrace[$i]['line'];
}
// More Compatibility because of E_USER_DEPRECATED
// http://www.php.net/manual/de/errorfunc.constants.php#87906
if (!defined('E_WARNING')) define('E_WARNING', 2);
if (!defined('E_NOTICE')) define('E_NOTICE', 8);
if (!defined('E_CORE_WARNING')) define('E_CORE_WARNING', 32);
if (!defined('E_USER_ERROR')) define('E_USER_ERROR', 256);
if (!defined('E_USER_WARNING')) define('E_USER_WARNING', 512);
if (!defined('E_USER_NOTICE')) define('E_USER_NOTICE', 1024);
if (!defined('E_USER_DEPRECATED')) define('E_USER_DEPRECATED', 16384);
$errortypes = array (
E_WARNING => 'Warning', // Useful for trigger_error use ?
E_NOTICE => 'Notice', // Useful for trigger_error use ?
E_CORE_WARNING => 'Core warning', // Useful for trigger_error use ?
E_USER_ERROR => 'User error',
E_USER_WARNING => 'User warning',
E_USER_NOTICE => 'User notice',
E_USER_DEPRECATED => 'User runtime notice'
);
if (isset($errortypes[$type])) {
$type = $errortypes[$type];
} else {
return false;
}
// $backtrace[$i+1] is the function that has called trigger_error()!
if (isset($backtrace[$i+1])) {
$function = $backtrace[$i+1]['function'];
$file = $backtrace[$i+1]['file'];
$line = $backtrace[$i+1]['line'];
} else {
$function = $backtrace[$i]['function'];
}
if (ini_get('display_errors')) {
if (ini_get('html_errors')) {
$template = '<br /><b>%s</b>: %s in <b>%s</b> on line <b>%s</b><br />';
} else {
$template = "\r\n%s: %s in %s on line %s\r\n";
}
echo sprintf(
$template,
$type,
($i == 0 || preg_match('/^[a-zA-Z0-9_]+\(.*?\)(?: \[.+\]):?/', $msg)) ? $msg : $function.'(): '.$msg,
htmlspecialchars($file),
(int) $line
);
}
if (ini_get('log_errors')) {
$msg = sprintf(
"[%s] PHP %s: %s in %s on line %s\n",
date('d-M-Y H-i-s', $_SERVER['REQUEST_TIME']),
$type,
$i == 0 || preg_match('/^[a-zA-Z0-9_]+\(.*?\)(?: \[.+\]):?/', $msg) ? preg_replace('/^([a-zA-Z0-9_]+\(.*?\))(?: \[.+\]):?/', '\1:', $msg) : $function.'() '.str_replace(array("\r", "\n", "\t"), array('\\r', '\\n', '\\t'), preg_replace('/^[a-zA-Z0-9_]+\(.*?\)(?: \[.+\]):?/', '', $msg)),
htmlspecialchars($file),
(int) $line
);
error_log($msg, 3, ini_get('error_log'));
}
if (ini_get('track_errors')) {
// Problems:
// - Usually, PHP doesn't allow ini_set('track_errors', true) in the runtime, but here, it will work...
// - Also, I cannot set the $php_errormsg in the correct scope as PHP does
global $php_errormsg;
$php_errormsg = $msg;
}
return true;
}
set_error_handler("myErrorHandler");
Daniel Marschall
ViaThinkSoft Co-Founder
ViaThinkSoft Co-Founder