[英]PHP error_log performance issues
我一直在嘗試編寫一個我可以在網站上使用的錯誤處理類,它會在發生錯誤時通過電子郵件發送給我。 問題是,當我分析應用程序時,它會阻塞error_log函數。 這是我的代碼(省略了這個類:
class ErrorHandler
{
private static $instance;
private static $mail;
private function __clone(){}
private function __construct()
{
error_reporting( E_ALL | E_STRICT );
if(!defined('ENV')){
if($_SERVER['SERVER_ADDR']=='127.0.0.1' || $_SERVER['SERVER_NAME']=='localhost')
{
#echo"local environment<br>";
DEFINE('ENV','LOCAL');
ini_set('display_errors', 1);
}
else
{
#echo"live environment";
DEFINE('ENV','LIVE');
ini_set('display_errors', 0);
}
}
}
public function setErrorConfig($error_level,$mail='',$mode='production')
{
error_reporting($error_level);
switch($mode)
{
case 'development':
ini_set('display_errors', '1');
break;
case 'production':
ini_set('display_errors', '0');
if($mail != ''){
self::$mail = $mail;
set_error_handler(array('ErrorHandler', 'handleError'));
}
break;
default:
ini_set('display_errors', '0');
error_reporting( E_ERROR );
break;
}
}
public function handleError($e_num,$e_msg,$e_file,$e_line,$e_vars)
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: DC_Research Site' . "\r\n";
$msg = '';
$msg .= '<html><head></head><body>';
$msg .= '<STYLE>h2{font-family:verdana;}</STYLE>';
$msg .= '<h2>Error Description:</h2>';
$msg .= '<h2>Script:</h2><p>'.$e_file.'</p>';
$msg .= '<h2>Line:</h2><p>'.$e_line.'</p>';
$msg .= '<h2>Message:</h2><p>'.$e_msg.'</p>';
$msg .= '<h2>Variables:</h2><p>'.$e_vars.'</p>';
$msg .= '</html></body>';
#mail(self::$mail,'Error Report',$msg,$headers);
error_log($msg,1,self::$mail,$headers);
}
}
你能幫我弄清楚是什么殺了它嗎?
根據定義,發送郵件是一項昂貴的操作(因為它很可能需要聯系SMTP服務器),因此當您對程序進行概要分析時,與在程序的其他行中花費的時間相比,在error_log中花費的時間將是巨大的。
您可以將錯誤信息存儲在數據庫中,然后讓cron腳本通過電子郵件向您發送內容。 我認為保存到數據庫對用戶來說比發送電子郵件更快
我終於解決了這個問題 - 我的壞。 嘗試將錯誤處理程序設置為僅記錄到郵件會導致腳本在本地設置上突然顯示 - 因為它找不到郵件服務器(我假設)。 在檢測位置的條件中包裝方法調用可以解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.