目前的TP框架雖然有了錯誤日志,但不支持監控報警,以下是我的報警機制
找到文档ThinkPHP\Library\Think\Exception.class.php
將
class Exception extends \Exception {
}修改爲
class Exception extends \Exception
{
public function __destruct(){
$xdebug_message = $this->message;
$xdebug_code = $this->code;
$xdebug_file = $this->file;
$xdebug_line = $this->line;
$xdebug_html = $this->__toString();
$sms_content = $xdebug_message . ' in ' . $xdebug_file . ' on line ' . $xdebug_line . ' at ' . __SELF__;
$email_content = nl2br($xdebug_html);
$path = realpath(LOG_PATH).'/error_report/';
if(!is_dir($path)) mkdir($path);
if(!is_dir($path.'sms')) mkdir($path.'sms');
if(!is_dir($path.'email')) mkdir($path.'email');
$datetime = date('Y-m-d_H-i-s').'_'.rand(1000,9999).'.log';
$sms_file = $path.'sms/'.$datetime;
$email_file = $path.'email/'.$datetime;
file_put_contents($sms_file,$sms_content);
file_put_contents($email_file,$email_content);
}
}找到文档ThinkPHP\Library\Think\Think.class.php 第286行
將
if (APP_DEBUG || IS_CLI) {
//調試模式下輸出錯誤信息
if (!is_array($error)) {
$trace = debug_backtrace();
$e['message'] = $error;
$e['file'] = $trace[0]['file'];
$e['line'] = $trace[0]['line'];
ob_start();
debug_print_backtrace();
$e['trace'] = ob_get_clean();
} else {
$e = $error;
}
if(IS_CLI){
exit(iconv('UTF-8','gbk',$e['message']).PHP_EOL.'FILE: '.$e['file'].'('.$e['line'].')'.PHP_EOL.$e['trace']);
}
} else {
//否則定向到錯誤頁面
$error_page = C('ERROR_PAGE');
if (!empty($error_page)) {
redirect($error_page);
} else {
$message = is_array($error) ? $error['message'] : $error;
$e['message'] = C('SHOW_ERROR_MSG')? $message : C('ERROR_MESSAGE');
}
}修改爲
// 獲取錯誤信息
if (!is_array($error)) {
$trace = debug_backtrace();
$e['message'] = $error;
$e['file'] = $trace[0]['file'];
$e['line'] = $trace[0]['line'];
ob_start();
debug_print_backtrace();
$e['trace'] = ob_get_clean();
} else {
$e = $error;
}
// 監測機制
$xdebug_type = '';
if(preg_match('/^'.L('_MODULE_NOT_EXIST_').'/',$e['message'])){
$xdebug_type = 'MODULE_NOT_EXIST';
}elseif(preg_match('/^'.L('_CONTROLLER_NOT_EXIST_').'/',$e['message'])){
$xdebug_type = 'CONTROLLER_NOT_EXIST';
}elseif(preg_match('/^'.L('_ERROR_ACTION_').'/',$e['message'])){
$xdebug_type = 'ERROR_ACTION';
}
$allow = C('ERROR_LISTEN_LEVEL');
if(empty($xdebug_type) || in_array($xdebug_type,explode(',',$allow))){
$content = $e['message'] . ' in ' . $e['file'] . ' on line ' . $e['line'] . ' at ' . __SELF__;
$econtent = $content.(isset($e['trace'])?'<br />'.$e['trace']:'');
// 寫入監測日志
$path = realpath(LOG_PATH).'/error_report/';
if(!is_dir($path)) mkdir($path);
if(!is_dir($path.'sms')) mkdir($path.'sms');
if(!is_dir($path.'email')) mkdir($path.'email');
$datetime = date('Y-m-d_H-i-s').'_'.rand(1000,9999).'.log';
$sms_file = $path.'sms/'.$datetime;
$email_file = $path.'email/'.$datetime;
C('ERROR_LISTEN_SMS') && file_put_contents($sms_file,$content);
C('ERROR_LISTEN_EMAIL') && file_put_contents($email_file,$econtent);
}
if (APP_DEBUG || IS_CLI) {
if(IS_CLI){
exit(iconv('UTF-8','gbk',$e['message']).PHP_EOL.'FILE: '.$e['file'].'('.$e['line'].')'.PHP_EOL.$e['trace']);
}
} else {
//否則定向到錯誤頁面
$error_page = C('ERROR_PAGE');
if (!empty($error_page)) {
redirect($error_page);
} else {
$message = is_array($error) ? $error['message'] : $error;
$e['message'] = C('SHOW_ERROR_MSG')? $message : C('ERROR_MESSAGE');
}
}這樣,當有錯誤的時候,就會在設定好的目錄裡生成相應的報警文档,我們只要監控這兩個目錄,並將相關信息發送到監控人郵箱或手機即可