Caution: minimum PHP 5.2
<?php
// ========================
// Log PHP
// ========================
// Chamada quando vai ter Error
function call_fatal_handler()
{
// Déf.
$err_file = "nao_sei";
$err_str = "shutdown";
$err_no = E_CORE_ERROR;
$err_line = 0;
// Qual e a ultima error?
$error = error_get_last();
// Se tem, podems ler dados
if( $error !== NULL)
{
$err_no = $error["type"]; // Tipo
$err_file = $error["file"]; // O documento
$err_line = $error["line"]; // a linha
$err_str = $error["message"]; // o mensagem de error
// Aqui, podemos criar um email, salvar um BDD, ....
$content = call_format_error( $err_no, $err_str, $err_file, $err_line );
echo "<br>".$content;
}
}
function call_format_error( $errno, $errstr, $errfile, $errline )
{
$trace = print_r( debug_backtrace( false ), true );
$content = "<b>Error: </b>".$errstr."<br>\n";
$content .= "<b>Errno: </b>".$errno."<br>\n";
$content .= "<b>File: </b>".$errfile."<br>\n";
$content .= "<b>Line: </b>".$errline."<br>\n";
$content .= "<b>Trace: </b><pre>".$trace."</pre><br>\n";
return $content;
}
//-------------------------------------------------------------------------
register_shutdown_function( "call_fatal_handler" );
echo "Ola";
//$a = strstr();
echo "Display".$a; // $a nao tem definiçao
?>
I forgot a VERY important point, if instead of displaying the result, you try to insert a BDD, it won’t work. Because as the function is called at the end of the script, at this point the connection to the database is closed. The solution is to make another "sql_open" before your INSERT.
I believe it would be best to send to a . html file that you have access to, to serve as a log. I say this because in theory, problems with the database may also occur.
– touchmx
What have you tried so far? You can already use flow control expressions like
try..catch
?– g.carvalho97
Using the Try catch block will not capture syntax errors or fatal errors.
– Gustavo Piucco
What do you want to log? Error type 404.. , Error SQL (type SELCT FROM TAB), PHP error type parameters missing etc. everything? Because it needs different "tactic". It also depends on the server. Are you in "local", with a dedicated server or with a server 'Mutual'? Ah a VERY important point to answer: the version of PHP you use.
– Peter
What is the difference between this question and your previous question? The most voted answer there responds to this one too. I’m closing as duplicate as we clarify this, ok?
– bfavaretto