Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom Script to manage Error Log in PHP
<?php
function customErrorHandler($errno, $errstr, $errfile, $errline) {
if (!(error_reporting() & $errno))
return true;
switch ($errno) {
case E_USER_ERROR:
echo "[client ".$_SERVER['REMOTE_ADDR']."] ERROR: [".$errno."] ".$errstr." on line ".$errline." in file ".$errfile." ";
exit(1);
break;
case E_USER_WARNING:
case E_USER_NOTICE:
default:
echo "[client ".$_SERVER['REMOTE_ADDR']."] ERROR: [".$errno."] ".$errstr." on line ".$errline." in file ".$errfile." ";
break;
}
error_log("[$errno] $errstr", 0);
return true;
}
function fatalErrorHandler() {
$last_error = error_get_last();
if ($last_error['type'] === E_ERROR)
customErrorHandler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']);
}
register_shutdown_function('fatalErrorHandler');
set_error_handler("customErrorHandler", E_ALL|E_NOTICE|E_STRICT|E_DEPRECATED);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment