Introduction
I wrote three functions that aims to log errors and exceptions as well as show a user a generic error page. Would appreciate some feedback on it with regards to the PSR standards and if I have left out anything of value.
Memory issue:
I am aware that the memory issue has not been addressed in this code, i.e. if you run out of memory the errors will not be logged. However, this isn't an issue I'm looking to solve at the moment.
Code:
<?php
// General error handling
error_reporting(-1);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
// Custom error handler
function errorHandler($type, $str, $file, $line) {
// Sets path to error log
$errorLog = "/home/ubuntu/errors.log";
// Generic error structure
$msg = $str . ' in ' . $file . ' on line ' . $line;
// Checks error severity
switch ($type) {
// Notices
case E_NOTICE:
case E_USER_NOTICE:
case E_DEPRECATED:
case E_USER_DEPRECATED:
case E_STRICT:
// Logs notices to file
$notice = 'NOTICE ' . $msg;
file_put_contents($errorLog, $notice . PHP_EOL, FILE_APPEND);
break;
// Warnings
case E_WARNING:
case E_USER_WARNING:
// Logs warnings to file
$warning = 'WARNING ' . $msg;
file_put_contents($errorLog, $warning . PHP_EOL, FILE_APPEND);
break;
// Unknown errors
default:
// Sets header and redirect user to generic error page
header('HTTP/1.1 500 Internal Server Error', true, 500);
require_once('500.php');
// Terminates the script
exit;
}
}
// Custom exception handler
function exceptionHandler($e) {
// Sets path to exception log
$exceptionLog = "/home/ubuntu/exception.log";
// Logs exceptions to file
file_put_contents($exceptionLog, $e->__toString() . PHP_EOL, FILE_APPEND);
// Sets header and redirect user to generic error page
header('HTTP/1.1 500 Internal Server Error', true, 500);
require_once('500.php');
// Terminates the script
exit;
}
// Logs on shutdown caused by an error
function fatalErrorHandler() {
// Gets the last occured error
$error = error_get_last();
// Check if error exists
if (!empty($error) && in_array($error['type'], array(E_ERROR, E_USER_ERROR))) {
// Sets path to error log
$fatalErrorLog = "/home/ubuntu/errors.log";
// Generic fatal error structure
$fatal = 'FATAL ' . $error['message'] . ' in ' . $error['file'] . ' on line ' . $error['line'];
// Logs fatal error to file
file_put_contents($fatalErrorLog, $fatal . PHP_EOL, FILE_APPEND);
// Sets header and redirect user to generic error page
header('HTTP/1.1 500 Internal Server Error', true, 500);
require_once('500.php');
// Terminates the script
exit;
}
}
// Sets custom handler for errors, exceptions and fatal errors
set_error_handler('errorHandler');
set_exception_handler('exceptionHandler');
register_shutdown_function('fatalErrorHandler');
?>