Last active
August 29, 2015 14:05
-
-
Save stefansundin/789cc36ccfc75d0ca243 to your computer and use it in GitHub Desktop.
Simple PHP error handler mailer.
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 | |
# https://gist.github.com/stefansundin/789cc36ccfc75d0ca243 | |
date_default_timezone_set("Europe/Stockholm"); | |
// edit this function to be notified of script errors by email | |
function mail_error($error) { | |
return; // comment this line out and edit this function if you want to use this | |
$error["timestamp"] = strftime("%Y-%m-%d %T %z"); | |
$error["ip"] = $_SERVER["REMOTE_ADDR"]; | |
$error["uri"] = $_SERVER["REQUEST_URI"]; | |
$headers = <<<EOF | |
From: php | |
Content-Type: text/plain; charset=utf-8 | |
EOF; | |
mail("you@example.com", "script-name.php error", print_r($error,true), $headers); | |
} | |
function log_exception($exception) { | |
mail_error(array( | |
"handler" => "exception", | |
"type" => $exception->getCode(), | |
"message" => $exception->getMessage(), | |
"file" => $exception->getFile(), | |
"line" => $exception->getLine(), | |
"trace" => $exception->getTrace() | |
)); | |
} | |
function log_error($errno, $errstr, $errfile, $errline) { | |
if (!error_reporting()) return true; | |
mail_error(array( | |
"handler" => "error", | |
"type" => $errno, | |
"message" => $errstr, | |
"file" => $errfile, | |
"line" => $errline | |
)); | |
return false; | |
} | |
function log_shutdown() { | |
$error = error_get_last(); | |
if ($error != NULL) { | |
$error["handler"] = "shutdown"; | |
mail_error($error); | |
} | |
} | |
set_exception_handler("log_exception"); | |
set_error_handler("log_error"); | |
register_shutdown_function("log_shutdown"); | |
// ini_set("display_errors", 0); // disable PHP's error handler | |
throw new Exception('PEBCAK'); // test exception handler | |
$test = $arnold+1; // test error handler | |
arnold(); // test shutdown handler |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment