Note that depending on the error, the execution of the script will be stopped. If at any time DB gave stopped and generated an error, you will not be able to generate the LOG, so usually the Logs are recorded in a simple TXT, with date-time, file, line, code and message.
Basically you need to use these 3 functions below. They capture all kinds of errors and trigger a Exception
which will be where you receive the error information and generates the LOG.
(register_shutdown_function)
Records a function to be executed at the end of the execution. Its function myLog
when it is executed you can check the 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)
Sets a user function for handling exceptions. Usually when a Exception is fired without capture. Its variable $exception
is an object and contains the properties of the class with the errors, can take a look at the 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)
Sets a user function to manipulate errors. When an error occurs, its function will identify and generate a log or trigger a Exception which will be captured by its above function 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' );