note 112291 added to function.set-error-handler
| From: | Philip | Date: | Tue, 28 May 2013 19:10:56 +0000 |
| Subject: | note 112291 added to function.set-error-handler | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194978@lists.php.net to get a copy of this message | ||
By this function alone you can not catch fatal errors, there is a simple work around. Below is part
of my error.php file which handles errors and exceptions in the application. Before someone
complains I'll add that I do not care that I am using globals, this file is part of my mini
framework and without the 'config' variable the application would crash anyways.
<?php
/**
* Error handler, passes flow over the exception logger with new ErrorException.
*/
function log_error( $num, $str, $file, $line, $context = null )
{
log_exception( new ErrorException( $str, 0, $num, $file, $line ) );
}
/**
* Uncaught exception handler.
*/
function log_exception( Exception $e )
{
global $config;
if ( $config["debug"] == true )
{
print "<div style='text-align: center;'>";
print "<h2 style='color: rgb(190, 50, 50);'>Exception
Occured:</h2>";
print "<table style='width: 800px; display: inline-block;'>";
print "<tr style='background-color:rgb(230,230,230);'><th
style='width: 80px;'>Type</th><td>" . get_class( $e ) .
"</td></tr>";
print "<tr
style='background-color:rgb(240,240,240);'><th>Message</th><td>{$e->getMessage()}</td></tr>";
print "<tr
style='background-color:rgb(230,230,230);'><th>File</th><td>{$e->getFile()}</td></tr>";
print "<tr
style='background-color:rgb(240,240,240);'><th>Line</th><td>{$e->getLine()}</td></tr>";
print "</table></div>";
}
else
{
$message = "Type: " . get_class( $e ) . "; Message: {$e->getMessage()};
File: {$e->getFile()}; Line: {$e->getLine()};";
file_put_contents( $config["app_dir"] . "/tmp/logs/exceptions.log",
$message . PHP_EOL, FILE_APPEND );
header( "Location: {$config["error_page"]}" );
}
exit();
}
/**
* Checks for a fatal error, work around for set_error_handler not working on fatal errors.
*/
function check_for_fatal()
{
$error = error_get_last();
if ( $error["type"] == E_ERROR )
log_error( $error["type"], $error["message"], $error["file"],
$error["line"] );
}
register_shutdown_function( "check_for_fatal" );
set_error_handler( "log_error" );
set_exception_handler( "log_exception" );
ini_set( "display_errors", "off" );
error_reporting( E_ALL );
----
Server IP: 87.124.126.49
Probable Submitter: 97.95.226.125
----
Manual Page -- http://php.net/manual/en/function.set-error-handler.php
Edit -- https://master.php.net/note/edit/112291
Del: integrated -- https://master.php.net/note/delete/112291/integrated
Del: useless -- https://master.php.net/note/delete/112291/useless
Del: bad code -- https://master.php.net/note/delete/112291/bad+code
Del: spam -- https://master.php.net/note/delete/112291/spam
Del: non-english -- https://master.php.net/note/delete/112291/non-english
Del: in docs -- https://master.php.net/note/delete/112291/in+docs
Del: other reasons-- https://master.php.net/note/delete/112291
Reject -- https://master.php.net/note/reject/112291
Search -- https://master.php.net/manage/user-notes.php