电商网站开发的目的是,龙岩网站建,ps做网站难吗,搭建网站平台需要多少钱什么是JCatch当程序发生异常(Exception)#xff0c;处理方式一般是通过日志文件记录下来#xff0c;这种方式很容易被忽略#xff0c;而且查询起来比较麻烦。JCatch提供了一种方案#xff0c;当程序发生异常时#xff0c;通过JCatch平台接口提交到JCatch平台#xff0c;由…什么是JCatch当程序发生异常(Exception)处理方式一般是通过日志文件记录下来这种方式很容易被忽略而且查询起来比较麻烦。JCatch提供了一种方案当程序发生异常时通过JCatch平台接口提交到JCatch平台由JCatch平台统一管理所有异常这样可以方便查询并且JCatch平台提供异常监控功能当应用程序发生异常时会以邮件方式提醒相关人员。应用列表:应用配置:异常列表:Java异常详情:PHP异常:邮件通知:运行/部署JCatchMySQL数据库使用MySQL数据库执行doc/sql.sql中的SQL脚本创建表修改src/main/resources/MyBatis.xml文件中的数据库连接配置登录管理员账户默认管理员账户 用户名 admin 密码 xxg管理员登录基于Spring Security实现可以配置src/main/resources/springSecurity.xml文件支持邮件订阅功能邮件订阅功能会在应用发生异常时每小时发送一封邮件给应用订阅者邮件内容为这个小时内的异常信息配置src/main/resources/messageService.xml支持邮件订阅打包mvn package命令生成war包可部署在Tomcat、Jetty等服务器中运行MyBatis Generator生成代码如果二次开发需要修改表结构, 修改src/main/resources/generatorConfig.xml文件中的数据库连接, com.xxg.jcatch.mbg包下代码是通过MyBatis Generator自动生成的可以运行mvn mybatis-generator:generate命令生成代码API提交一条ExceptionURL: http://[your baseUrl]/api/submitExceptionJson?appId[your appId]请求类型: POST请求Body:{fileName: Main.java,methodName: main,className: com.xxg.jcatch.Main,stackTrace: java.lang.ArithmeticException: / by zero\n\tat com.xxg.jcatch.Main.main(Main.java:16)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat java.lang.reflect.Method.invoke(Method.java:498)\n\tat com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)\n,message: / by zero,lineNumber: 16,exceptionName: java.lang.ArithmeticException}响应Body:{success: true}客户端接入JavaPHP通用function submitToJCatch($e) {$json json_encode($e);$ch curl_init(http:///api/submitExceptionJson?appId);curl_setopt_array($ch, array(CURLOPT_POST TRUE,CURLOPT_RETURNTRANSFER TRUE,CURLOPT_HTTPHEADER array(Content-Type: application/json),CURLOPT_POSTFIELDS $json,CURLOPT_CONNECTTIMEOUT 2,CURLOPT_TIMEOUT 4,));curl_exec($ch);}function error_handler($errno, $errstr, $errfile, $errline) {submitToJCatch(array(message $errstr,fileName $errfile,lineNumber $errline,));}function exception_handler($exception) {submitToJCatch(array(fileName $exception-getFile(),lineNumber $exception-getLine(),stackTrace $exception-getTraceAsString(),message $exception-getMessage(),exceptionName get_class($exception)));}set_error_handler(error_handler);set_exception_handler(exception_handler);PHP的错误和异常会通过set_error_handler、set_exception_handler指定的函数来处理一般PHP框架都会有自己的一套错误处理机制如果使用PHP框架建议通过框架本身的错误处理机制来提交错误到JCatch下面有各个框架接入JCatch的方法。Laravel修改App/Exceptions/Handler.php文件:public function report(Exception $exception){$appId ;$submitUrl http:///api/submitExceptionJson?appId . $appId;$data json_encode([fileName $exception-getFile(),lineNumber $exception-getLine(),stackTrace $exception-getTraceAsString(),message $exception-getMessage(),exceptionName get_class($exception)]);$ch curl_init($submitUrl);curl_setopt_array($ch, array(CURLOPT_POST TRUE,CURLOPT_RETURNTRANSFER TRUE,CURLOPT_HTTPHEADER array(Content-Type: application/json),CURLOPT_POSTFIELDS $data,CURLOPT_CONNECTTIMEOUT 2,CURLOPT_TIMEOUT 4,));curl_exec($ch);return parent::report($exception);}CodeIgniter 3.x在application/core目录下新建文件MY_Exceptions.php代码如下class MY_Exceptions extends CI_Exceptions{public function log_exception($severity, $message, $filepath, $line){parent::log_exception($severity, $message, $filepath, $line);$json json_encode(array(message $message,fileName $filepath,lineNumber $line));$ch curl_init(http:///api/submitExceptionJson?appId);curl_setopt_array($ch, array(CURLOPT_POST TRUE,CURLOPT_RETURNTRANSFER TRUE,CURLOPT_HTTPHEADER array(Content-Type: application/json),CURLOPT_POSTFIELDS $json,CURLOPT_CONNECTTIMEOUT 2,CURLOPT_TIMEOUT 4,));curl_exec($ch);}}当发生错误或异常时CI框架会自动调用以上代码将异常信息提交到JCatch API。Yii 1.1在protected/components目录下新建文件ErrorHandler.php代码如下class ErrorHandler extends CErrorHandler{public $jcatchBaseUrl;public $jcatchAppId;public $jcatchSecretKey;protected function handleException($exception){parent::handleException($exception);$this-submitToJCatch(array(fileName $exception-getFile(),lineNumber $exception-getLine(),stackTrace $exception-getTraceAsString(),message $exception-getMessage(),exceptionName get_class($exception)));}protected function handleError($event){parent::handleError($event);$this-submitToJCatch(array(message $event-message,fileName $event-file,lineNumber $event-line,));}private function submitToJCatch($e) {$json json_encode($e);$ch curl_init($this-jcatchBaseUrl./api/submitExceptionJson?appId.$this-jcatchAppId);curl_setopt_array($ch, array(CURLOPT_POST TRUE,CURLOPT_RETURNTRANSFER TRUE,CURLOPT_HTTPHEADER array(Content-Type: application/json),CURLOPT_POSTFIELDS $json,CURLOPT_CONNECTTIMEOUT 2,CURLOPT_TIMEOUT 4,));curl_exec($ch);}}修改所使用的配置文件例如protected/config/main.phperrorHandlerarray(errorActionsite/error,classErrorHandler,jcatchBaseUrl,jcatchAppId,jcatchSecretKey,),当发生错误或异常时Yii框架会自动调用ErrorHandler来处理异常将异常信息提交到JCatch API。