🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
#### 异常捕捉 swoolefy在每个worker进程和task进程启动时,会创建一个单一应用对象,同时会完成注册自定义捕捉事件。 例如http的App.php中 ~~~ /** * $ExceptionHanderClass 异常处理类 * @var string */ private $ExceptionHanderClass = 'Swoolefy\\Core\\SwoolefyException'; /** * __construct * @param $config 应用层配置 */ public function __construct(array $config=[]) { // 将应用层配置保存在上下文的服务 $this->config = Swfy::$appConfig = $config; // Component组件创建 parent::creatObject(); // 注册错误处理事件 $exceptionClass = $this->getExceptionClass(); register_shutdown_function($exceptionClass.'::fatalError'); set_error_handler($exceptionClass.'::appError'); } ~~~ 默认会使用swoolefy框架内置的`Swoolefy\Core\SwoolefyException`类处理异常捕捉事件。 例如默认处理如下: ~~~ /** * shutHalt 错误输出日志 * @param $error 错误 * @return void */ public static function shutHalt($errorMsg, $errorType = 'error') { $logFilePath = rtrim(LOG_PATH,'/').'/runtime.log'; if(is_file($logFilePath)) { $logFilesSize = filesize($logFilePath); } // 定时清除这个log文件 if($logFilesSize > 1024 * 20) { @file_put_contents($logFilePath,''); } $log = new \Swoolefy\Tool\Log; switch($errorType) { case 'error': $log->setChannel('Application')->setLogFilePath($logFilePath)->addError($errorMsg); break; case 'warning': $log->setChannel('Application')->setLogFilePath($logFilePath)->addWarning($errorMsg); break; case 'notice': $log->setChannel('Application')->setLogFilePath($logFilePath)->addNotice($errorMsg); break; case 'info': $log->setChannel('Application')->setLogFilePath($logFilePath)->addInfo($errorMsg); break; } return; } ~~~ 那么我们可以自定义自己的异常处理,配置项" exception_handler"是一个异常处理类,只需要在应用层,继承于`Swoolefy\Core\SwoolefyException`,可以重写shutHalt函数,然后处理。比如,可以在这里处理接入日志系统graylog等。 <table><tr><td bgcolor=orange> 特别注意: 如果需要重写,同时需要在协议层的配置文件中配置exception_handler,值为我们新定义的类,这个类要继承Swoolefy\Core\SwoolefyException </td></tr></table>