If a fatal error occurs catch block calls
logError
function and passes details of the error, such as:
file
,
line
,
code
,
message
and
trace
try {
undefinedFunction();
} catch (Error $e) {
logError(
'ERR FILE: ' . $e->getFile() . "\n" .
'ERR LINE: ' . $e->getLine() . "\n" .
'ERR CODE: ' . $e->getCode() . "\n" .
'ERR TEXT: ' . $e->getMessage() . "\n" .
'ERR TRACE String: ' . $e->getTraceAsString()
);
}
The function
logError
appends current date and time to the error details and logs into the error file
function logError($error) {
$content = "\n\n\n".date('Y-m-d H:i:s');
$content .= "\n".$error;
file_put_contents('/var/log/errors.txt', $content, FILE_APPEND);
}