Log fatal errors using register_shutdown_function
, which requires PHP 5.2+:
register_shutdown_function( "fatal_handler" );
function fatal_handler() {
$errfile = "unknown file";
$errstr = "shutdown";
$errno = E_CORE_ERROR;
$errline = 0;
$error = error_get_last();
if( $error !== NULL) {
$errno = $error["type"];
$errfile = $error["file"];
$errline = $error["line"];
$errstr = $error["message"];
error_mail(format_error( $errno, $errstr, $errfile, $errline));
}
}
You will have to define the error_mail
and format_error
functions. For example:
function format_error( $errno, $errstr, $errfile, $errline ) {
$trace = print_r( debug_backtrace( false ), true );
$content = "<table><thead bgcolor='#c8c8c8'><th>Item</th><th>Description</th></thead><tbody>";
$content .= "<tr valign='top'><td><b>Error</b></td><td><pre>$errstr</pre></td></tr>";
$content .= "<tr valign='top'><td><b>Errno</b></td><td><pre>$errno</pre></td></tr>";
$content .= "<tr valign='top'><td><b>File</b></td><td>$errfile</td></tr>";
$content .= "<tr valign='top'><td><b>Line</b></td><td>$errline</td></tr>";
$content .= "<tr valign='top'><td><b>Trace</b></td><td><pre>$trace</pre></td></tr>";
$content .= '</tbody></table>';
return $content;
}
function format_error( $errno, $errstr, $errfile, $errline ) {
$trace = print_r( debug_backtrace( false ), true );
$content = "
<table>
<thead><th>Item</th><th>Description</th></thead>
<tbody>
<tr>
<th>Error</th>
<td><pre>$errstr</pre></td>
</tr>
<tr>
<th>Errno</th>
<td><pre>$errno</pre></td>
</tr>
<tr>
<th>File</th>
<td>$errfile</td>
</tr>
<tr>
<th>Line</th>
<td>$errline</td>
</tr>
<tr>
<th>Trace</th>
<td><pre>$trace</pre></td>
</tr>
</tbody>
</table>";
return $content;
}
Use Swift Mailer to write the error_mail
function.
See also: