Note that depending on the error, the execution of the script will be interrupted. If at any time DB has stopped and generated an error, you will not be able to generate the LOG, so usually the LOGs are written in a simple TXT, with date-time, file, line, code and message.
Basically you need to use these 3 functions below. They catch all types of errors and fire a Exception
which will be where you receive the error information and generates the LOG.
(register_shutdown_function) sub>
Registers a function to execute at the end of execution. Its myLog
fault function when executed can check for errors using error_get_last()
function myLog()
{
if( ! is_null( $error = error_get_last() ) )
{
// obtendo informações do erro e disparando uma Exception
extract( $error , EXTR_SKIP );
new Exception( $message , $type , 0 , $file , $line );
}
}
register_shutdown_function( 'myLog' );
(set_exception_handler) sub>
Defines a user role for handling exceptions. Usually when an exception is fired without capture. Your $exception
variable is an object and contains class properties with errors, you can take a look at DOC Exception
function myException( $exception )
{
// mensagem de erro, grave um TXT-LOG com os dados do erro
echo '<h2>Error</h2><h3>Mensagem:</h3><pre>' . $exception->getMessage() . '</pre>
<h3>Arquivo:</h3><pre>' . $exception->getLine() . '</pre>
<h3>Linha:</h3><pre>' . $exception->getLine() . '</pre>
<h3>Código:</h3><pre>' . $exception->getCode() . '</pre>';
}
set_exception_handler( 'myException' );
(set_error_handler) sub>
Defines a user's role to handle errors. When an error occurs, its function will identify and generate a log or trigger an exception that will be captured by its function above myException
.
function log_error( $code , $error , $file , $line )
{
// obtendo informações do erro e disparando uma Exception
if( error_reporting() === 0 ) return;
new Exception( $error , $code , 0 , $file , $line );
}
set_error_handler( 'log_error' );