PHP

让TP支持运行监控

字号+ 编辑: 国内TP粉 修订: 秃顶萧峰 来源: ThinkPHP 2023-09-09 我要说两句(0)

目前的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');
            }
        }

这样,当有错误的时候,就会在设定好的目录里生成相应的报警文件,我们只要监控这两个目录,并将相关信息发送到监控人邮箱或手机即可

阅完此文,您的感想如何?
  • 有用

    1

  • 没用

    0

  • 开心

    0

  • 愤怒

    0

  • 可怜

    0

1.如文章侵犯了您的版权,请发邮件通知本站,该文章将在24小时内删除;
2.本站标注原创的文章,转发时烦请注明来源;
3.交流群: PHP+JS聊天群

相关课文
  • mac开发接入微信公众号接口返回报错 cURL error 56: SSLRead() return error -9806

  • pecl安装程序时报错Array and string offset access syntax with curly braces is no longer supported

  • PHP的换行符是什么

  • 由于商家传入的H5交易参数有误,该笔交易暂时无法完成,请联系商家解决

我要说说
网上宾友点评