Log 500 errors to a file

Dani 1 Tallied Votes 2K Views Share

You might be familiar with the dreaded blank page when your PHP script doesn't work.

Here's how to spit out errors to the screen, instead of getting just a blank page, as well as logging errors to a file.

// Spit out errors to the screen
error_reporting(-1);
ini_set('display_errors', 1);

register_shutdown_function('_log_error');

function _log_error()
{
    if (!empty($error = error_get_last()))
    {
        $output =
			date('Y-m-d H:i:s') . ' --> ' .
			$error['file'] . ' --> ' .
			$error['message'] . ' ' .
			$error['line'] .
			(isset($_SERVER['REQUEST_URI']) ? ' ' . $_SERVER['REQUEST_URI'] : '')
		;
        file_put_contents('fatal_errors.log', "$output\n", FILE_APPEND);
    }
}