目前的TP框架虽然有了错误日志,但不支持监控报警
目前的TP框架虽然有了错误日志,但不支持监控报警,以下是我的报警机制
找到文件
ThinkPHP\Library\Think\Exception.class.php
将class Exception extends \Exception {<br>
}
修改为class Exception extends \Exception {<br>
public function __destruct(){<br>
$xdebug_message = $this->message;<br>
$xdebug_code = $this->code;<br>
$xdebug_file = $this->file;<br>
$xdebug_line = $this->line;<br>
$xdebug_html = $this->__toString();<br>
<br>
$sms_content = $xdebug_message . ' in ' . $xdebug_file . ' on line ' . $xdebug_line . ' at ' . __SELF__;<br>
$email_content = nl2br($xdebug_html);<br>
<br>
$path = realpath(LOG_PATH).'/error_report/';<br>
if(!is_dir($path)) mkdir($path);<br>
if(!is_dir($path.'sms')) mkdir($path.'sms');<br>
if(!is_dir($path.'email')) mkdir($path.'email');<br>
$datetime = date('Y-m-d_H-i-s').'_'.rand(1000,9999).'.log';<br>
$sms_file = $path.'sms/'.$datetime;<br>
$email_file = $path.'email/'.$datetime;<br>
file_put_contents($sms_file,$sms_content);<br>
file_put_contents($email_file,$email_content);<br>
}<br>
}
找到文件
ThinkPHP\Library\Think\Think.class.php 第286行
将 if (APP_DEBUG || IS_CLI) {<br>
//调试模式下输出错误信息<br>
if (!is_array($error)) {<br>
$trace = debug_backtrace();<br>
$e['message'] = $error;<br>
$e['file'] = $trace[0]['file'];<br>
$e['line'] = $trace[0]['line'];<br>
ob_start();<br>
debug_print_backtrace();<br>
$e['trace'] = ob_get_clean();<br>
} else {<br>
$e = $error;<br>
}<br>
if(IS_CLI){<br>
exit(iconv('UTF-8','gbk',$e['message']).PHP_EOL.'FILE: '.$e['file'].'('.$e['line'].')'.PHP_EOL.$e['trace']);<br>
}<br>
} else {<br>
//否则定向到错误页面<br>
$error_page = C('ERROR_PAGE');<br>
if (!empty($error_page)) {<br>
redirect($error_page);<br>
} else {<br>
$message = is_array($error) ? $error['message'] : $error;<br>
$e['message'] = C('SHOW_ERROR_MSG')? $message : C('ERROR_MESSAGE');<br>
}<br>
}
修改为 //获取错误信息<br>
if (!is_array($error)) {<br>
$trace = debug_backtrace();<br>
$e['message'] = $error;<br>
$e['file'] = $trace[0]['file'];<br>
$e['line'] = $trace[0]['line'];<br>
ob_start();<br>
debug_print_backtrace();<br>
$e['trace'] = ob_get_clean();<br>
} else {<br>
$e = $error;<br>
}<br>
// 监测机制<br>
$xdebug_type = '';<br>
if(preg_match('/^'.L('_MODULE_NOT_EXIST_').'/',$e['message'])){<br>
$xdebug_type = 'MODULE_NOT_EXIST';<br>
}elseif(preg_match('/^'.L('_CONTROLLER_NOT_EXIST_').'/',$e['message'])){<br>
$xdebug_type = 'CONTROLLER_NOT_EXIST';<br>
}elseif(preg_match('/^'.L('_ERROR_ACTION_').'/',$e['message'])){<br>
$xdebug_type = 'ERROR_ACTION';<br>
}<br>
$allow = C('ERROR_LISTEN_LEVEL');<br>
if(empty($xdebug_type) || in_array($xdebug_type,explode(',',$allow))){<br>
$content = $e['message'] . ' in ' . $e['file'] . ' on line ' . $e['line'] . ' at ' . __SELF__;<br>
$econtent = $content.(isset($e['trace'])?'<br>'.$e['trace']:'');<br>
// 写入监测日志<br>
$path = realpath(LOG_PATH).'/error_report/';<br>
if(!is_dir($path)) mkdir($path);<br>
if(!is_dir($path.'sms')) mkdir($path.'sms');<br>
if(!is_dir($path.'email')) mkdir($path.'email');<br>
$datetime = date('Y-m-d_H-i-s').'_'.rand(1000,9999).'.log';<br>
$sms_file = $path.'sms/'.$datetime;<br>
$email_file = $path.'email/'.$datetime;<br>
C('ERROR_LISTEN_SMS') && file_put_contents($sms_file,$content);<br>
C('ERROR_LISTEN_EMAIL') && file_put_contents($email_file,$econtent);<br>
}<br>
<br>
if (APP_DEBUG || IS_CLI) {<br>
if(IS_CLI){<br>
exit(iconv('UTF-8','gbk',$e['message']).PHP_EOL.'FILE: '.$e['file'].'('.$e['line'].')'.PHP_EOL.$e['trace']);<br>
}<br>
} else {<br>
//否则定向到错误页面<br>
$error_page = C('ERROR_PAGE');<br>
if (!empty($error_page)) {<br>
redirect($error_page);<br>
} else {<br>
$message = is_array($error) ? $error['message'] : $error;<br>
$e['message'] = C('SHOW_ERROR_MSG')? $message : C('ERROR_MESSAGE');<br>
}<br>
}
这样,当有错误的时候,就会在设定好的目录里生成相应的报警文件,我们只要监控这两个目录,并将相关信息发送到监控人邮箱或手机即可
AD:真正免费,域名+虚机+企业邮箱=0元