How to capture errors and exceptions to send to BD with PHP?

Asked

Viewed 405 times

2

How can we capture all errors and exceptions generated at runtime in PHP, sending to the database to later make an organized log page?

Currently I redirect all requests to index.php, treating the URL in a friendly way with a MVC model, so in this case it would be possible to register some function/callback directly in index.php avoiding having to call this function/callback in all files, classes and functions?

  • 1

    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.

  • What have you tried so far? You can already use flow control expressions like try..catch?

  • Using the Try catch block will not capture syntax errors or fatal errors.

  • 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.

  • 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?

1 answer

1

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.

  • If that function has an error, then it would generate a loop infinite of errors? For example, the variable $content is being concatenated without being declared, it would not generate a NOTICE?

  • Exactly!! I made the modification. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.