From 5eb3caacb6160a3019d049e93479c89680958672 Mon Sep 17 00:00:00 2001 From: dxkite Date: Mon, 1 Jul 2019 21:05:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=9A=E5=BC=82=E5=B8=B8du?= =?UTF-8?q?mper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- suda/src/application/Application.php | 16 +++++++++++-- suda/src/application/DebugDumper.php | 30 ++++++++++++++++++++++++ suda/src/framework/debug/ConfigTrait.php | 4 +++- suda/src/framework/debug/DebugObject.php | 4 ++-- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/suda/src/application/Application.php b/suda/src/application/Application.php index 3363a7bd..a92ded64 100644 --- a/suda/src/application/Application.php +++ b/suda/src/application/Application.php @@ -26,6 +26,10 @@ */ class Application extends ApplicationSource { + /** + * @var DebugDumper + */ + protected $dumper; /** * 准备运行环境 @@ -67,8 +71,8 @@ protected function prepare(Request $request, Response $response) $response->getWrapper()->register(ExceptionContentWrapper::class, [Throwable::class]); $response->getWrapper()->register(TemplateWrapper::class, [RawTemplate::class]); - $debugDumper = new DebugDumper($this, $request, $response); - $debugDumper->register(); + $this->dumper = new DebugDumper($this, $request, $response); + $this->dumper->register(); $this->debug->info('{request-time} {remote-ip} {request-method} {request-uri} debug={debug}', [ 'remote-ip' => $request->getRemoteAddr(), @@ -84,6 +88,13 @@ protected function prepare(Request $request, Response $response) } } + /** + * @param Throwable $throwable + */ + public function dumpException($throwable) { + $this->dumper->dumpThrowable($throwable); + } + /** * 运行程序 * @@ -109,6 +120,7 @@ public function run(Request $request, Response $response) $this->debug->timeEnd('sending response'); } catch (Throwable $e) { $this->debug->uncaughtException($e); + $this->dumper->dumpThrowable($e); $response->sendContent($e); $response->end(); $this->debug->timeEnd('sending response'); diff --git a/suda/src/application/DebugDumper.php b/suda/src/application/DebugDumper.php index 6434c1f5..54a5d498 100644 --- a/suda/src/application/DebugDumper.php +++ b/suda/src/application/DebugDumper.php @@ -2,6 +2,9 @@ namespace suda\application; use Exception; +use phpDocumentor\Reflection\File; +use suda\framework\debug\DebugObject; +use suda\framework\filesystem\FileSystem; use suda\framework\Request; use suda\framework\Response; use Throwable; @@ -63,9 +66,36 @@ public function uncaughtException($exception) { $this->application->debug()->addIgnorePath(__FILE__); $this->application->debug()->uncaughtException($exception); + $this->dumpThrowable($exception); if ($this->response->isSend() === false) { $this->response->sendContent($exception); $this->response->end(); } } + + /** + * @param Throwable $throwable + */ + public function dumpThrowable($throwable) + { + $dumper = [ + 'time' => time(), + 'throwable' => $throwable, + 'context' => [ + 'application' => $this->application, + 'request' => $this->request, + 'response' => $this->response, + ], + 'backtrace' => $throwable->getTrace(), + ]; + $dumpPath = $this->application->getDataPath().'/logs/dump'; + $exceptionHash = md5($throwable->getFile().$throwable->getLine().$throwable->getCode()); + $path = $dumpPath.'/'.microtime(true).'.'.substr($exceptionHash, 0, 8).'.json'; + FileSystem::make($dumpPath); + FileSystem::put($path, json_encode( + new DebugObject($dumper), + JSON_PRETTY_PRINT + | JSON_UNESCAPED_UNICODE + )); + } } diff --git a/suda/src/framework/debug/ConfigTrait.php b/suda/src/framework/debug/ConfigTrait.php index 645d2ca7..85ab2622 100644 --- a/suda/src/framework/debug/ConfigTrait.php +++ b/suda/src/framework/debug/ConfigTrait.php @@ -18,7 +18,9 @@ public function applyConfig(array $config) $this->config[$name] = $config[$name] ?? $this->config[$name] ?? $value; } } - public function getConfig(string $name) { + + public function getConfig(string $name) + { $defaultConfig = $this->getDefaultConfig(); return $this->config[$name] ?? $defaultConfig[$name]; } diff --git a/suda/src/framework/debug/DebugObject.php b/suda/src/framework/debug/DebugObject.php index 802990ef..0af2cb76 100644 --- a/suda/src/framework/debug/DebugObject.php +++ b/suda/src/framework/debug/DebugObject.php @@ -48,12 +48,12 @@ protected function parseObject($object) { $objectHash = spl_object_hash($object); if ($this->isObjectExported($objectHash)) { - return ['_type' => get_class($object), '_address' => $objectHash]; + return ['_type' => get_class($object), '_hash' => $objectHash]; } $this->setObjectIsExported($objectHash); return [ '_type' => get_class($object), - '_address' => $objectHash, + '_hash' => $objectHash, '_properties' => $this->getObjectProp($object) ]; }