目前的TP框架虽然有了错误日志,但不支持监控报警
目前的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');
}
}
这样,当有错误的时候,就会在设定好的目录里生成相应的报警文件,我们只要监控这两个目录,并将相关信息发送到监控人邮箱或手机即可