让TP支持运行监控

jerry thinkphp 2015年11月19日 收藏
目前的TP框架虽然有了错误日志,但不支持监控报警
目前的TP框架虽然有了错误日志,但不支持监控报警,以下是我的报警机制
找到文件
ThinkPHP\Library\Think\Exception.class.php

  1. class Exception extends \Exception {
  2. }
修改为
  1. class Exception extends \Exception {
  2.     public function __destruct(){
  3.         $xdebug_message = $this->message;
  4.         $xdebug_code = $this->code;
  5.         $xdebug_file = $this->file;
  6.         $xdebug_line = $this->line;
  7.         $xdebug_html = $this->__toString();
  8.         
  9.         $sms_content = $xdebug_message . ' in ' . $xdebug_file . ' on line ' . $xdebug_line . ' at ' . __SELF__;
  10.         $email_content = nl2br($xdebug_html);
  11.         
  12.         $path = realpath(LOG_PATH).'/error_report/';
  13.         if(!is_dir($path)) mkdir($path);
  14.         if(!is_dir($path.'sms')) mkdir($path.'sms');
  15.         if(!is_dir($path.'email')) mkdir($path.'email');
  16.         $datetime = date('Y-m-d_H-i-s').'_'.rand(1000,9999).'.log';
  17.         $sms_file = $path.'sms/'.$datetime;
  18.         $email_file = $path.'email/'.$datetime;
  19.         file_put_contents($sms_file,$sms_content);
  20.         file_put_contents($email_file,$email_content);
  21.     }
  22. }
找到文件
ThinkPHP\Library\Think\Think.class.php 第286行

  1.         if (APP_DEBUG || IS_CLI) {
  2.             //调试模式下输出错误信息
  3.             if (!is_array($error)) {
  4.                 $trace          = debug_backtrace();
  5.                 $e['message']   = $error;
  6.                 $e['file']      = $trace[0]['file'];
  7.                 $e['line']      = $trace[0]['line'];
  8.                 ob_start();
  9.                 debug_print_backtrace();
  10.                 $e['trace']     = ob_get_clean();
  11.             } else {
  12.                 $e              = $error;
  13.             }
  14.             if(IS_CLI){
  15.                 exit(iconv('UTF-8','gbk',$e['message']).PHP_EOL.'FILE: '.$e['file'].'('.$e['line'].')'.PHP_EOL.$e['trace']);
  16.             }
  17.         } else {
  18.             //否则定向到错误页面
  19.             $error_page         = C('ERROR_PAGE');
  20.             if (!empty($error_page)) {
  21.                 redirect($error_page);
  22.             } else {
  23.                 $message        = is_array($error) ? $error['message'] : $error;
  24.                 $e['message']   = C('SHOW_ERROR_MSG')? $message : C('ERROR_MESSAGE');
  25.             }
  26.         }
修改为
  1.         //获取错误信息
  2.         if (!is_array($error)) {
  3.             $trace          = debug_backtrace();
  4.             $e['message']   = $error;
  5.             $e['file']      = $trace[0]['file'];
  6.             $e['line']      = $trace[0]['line'];
  7.             ob_start();
  8.             debug_print_backtrace();
  9.             $e['trace']     = ob_get_clean();
  10.         } else {
  11.             $e              = $error;
  12.         }
  13.         // 监测机制
  14.         $xdebug_type = '';
  15.         if(preg_match('/^'.L('_MODULE_NOT_EXIST_').'/',$e['message'])){
  16.             $xdebug_type = 'MODULE_NOT_EXIST';
  17.         }elseif(preg_match('/^'.L('_CONTROLLER_NOT_EXIST_').'/',$e['message'])){
  18.             $xdebug_type = 'CONTROLLER_NOT_EXIST';
  19.         }elseif(preg_match('/^'.L('_ERROR_ACTION_').'/',$e['message'])){
  20.             $xdebug_type = 'ERROR_ACTION';
  21.         }
  22.         $allow = C('ERROR_LISTEN_LEVEL');
  23.         if(empty($xdebug_type) || in_array($xdebug_type,explode(',',$allow))){
  24.             $content = $e['message'] . ' in ' . $e['file'] . ' on line ' . $e['line'] . ' at ' . __SELF__;
  25.             $econtent = $content.(isset($e['trace'])?'<br />'.$e['trace']:'');
  26.             // 写入监测日志
  27.             $path = realpath(LOG_PATH).'/error_report/';
  28.             if(!is_dir($path)) mkdir($path);
  29.             if(!is_dir($path.'sms')) mkdir($path.'sms');
  30.             if(!is_dir($path.'email')) mkdir($path.'email');
  31.             $datetime = date('Y-m-d_H-i-s').'_'.rand(1000,9999).'.log';
  32.             $sms_file = $path.'sms/'.$datetime;
  33.             $email_file = $path.'email/'.$datetime;
  34.             C('ERROR_LISTEN_SMS') && file_put_contents($sms_file,$content);
  35.             C('ERROR_LISTEN_EMAIL') && file_put_contents($email_file,$econtent);
  36.         }
  37.         
  38.         if (APP_DEBUG || IS_CLI) {
  39.             if(IS_CLI){
  40.                 exit(iconv('UTF-8','gbk',$e['message']).PHP_EOL.'FILE: '.$e['file'].'('.$e['line'].')'.PHP_EOL.$e['trace']);
  41.             }
  42.         } else {
  43.             //否则定向到错误页面
  44.             $error_page         = C('ERROR_PAGE');
  45.             if (!empty($error_page)) {
  46.                 redirect($error_page);
  47.             } else {
  48.                 $message        = is_array($error) ? $error['message'] : $error;
  49.                 $e['message']   = C('SHOW_ERROR_MSG')? $message : C('ERROR_MESSAGE');
  50.             }
  51.         }
这样,当有错误的时候,就会在设定好的目录里生成相应的报警文件,我们只要监控这两个目录,并将相关信息发送到监控人邮箱或手机即可