私がしたことは
DB エラーが発生したということは、単純なエラーか、MySQL サーバーがなくなったなどの致命的なエラーである可能性があることを意味します。その時点でデータベースにアクセスしようとしても無駄です。
- その場でエラー ログ ファイルを作成します。
- ファイル サイズが 0 バイト/存在しない場合は、電子メールをトリガーし、新しく作成されたファイルにエラーを記録します。
- さらにエラーが発生した場合は、すべてログに記録します。
- エラーに対処した後、ファイルをクリア/削除します。
- 次にエラーが発生すると、再度メールが届きます。
ファイルを削除する唯一の手動作業があることは知っています。しかし、この方法を使用することで、多くの問題を解決できます。
/**
* THIS FUNCTION LOGS THE ERRORS TO THE FILE
* @param string $error_details Details of The Error
* @return bool
*/
function saveErrorToFile($error_details){
$file_path = JPATH_COMPONENT_ADMINISTRATOR.DS.'helpers'.DS.'error_log.txt';
if(!JFile::exists($file_path ))
{
//create the file and write the error
JFile::write($file_path,$error_details);
//send the mail with the error
$mail =& JFactory::getMailer();
$mail->setSender(array('support@abc.com', 'support'));
$mail->setSubject('DATABASE ERROR');
$mail->setBody($error_details);
$mail->IsHTML(true);
$mail->addRecipient('dasun@abc.com');
$mail->Send();
}
else
{
//read the existing content of the file and append with the new error
$file_content = JFile::read($file_path);
$file_content = $file_content."\n\n".$error_details;
JFile::write($file_path,$file_content);
}
return true;
}