Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple PHP error handler mailer.
<?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