diff --git a/convention.php b/convention.php index 3ad29bb5..efcf27fc 100644 --- a/convention.php +++ b/convention.php @@ -140,7 +140,7 @@ return [ // +---------------------------------------------------------------------- 'log' => [ - // 日志记录方式,支持 file socket sae + // 日志记录方式,支持 file sae 'type' => 'File', // 日志保存目录 'path' => LOG_PATH, @@ -150,7 +150,7 @@ return [ // | Trace设置 // +---------------------------------------------------------------------- 'trace' => [ - //支持Html,Console 设为false则不显示 + // 支持Html Socket Console 设为false则不显示 'type' => false, ], diff --git a/library/think/App.php b/library/think/App.php index b881f921..b6438d04 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -12,7 +12,6 @@ namespace think; use think\Config; -use think\debug\Trace; use think\Exception; use think\exception\HttpException; use think\exception\HttpResponseException; @@ -145,19 +144,18 @@ class App $response = $data; } elseif (!is_null($data)) { // 默认自动识别响应输出类型 - $isAjax = $request->isAjax(); - $type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type'); + $isAjax = $request->isAjax(); + $type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type'); $response = Response::create($data, $type); } else { $response = Response::create(); } - // 监听app_end Hook::listen('app_end', $response); - - //注入Trace - self::$debug && Trace::inject($response); + + // Trace调试注入 + Debug::inject($response); return $response; } diff --git a/library/think/Debug.php b/library/think/Debug.php index 133d1d66..92ad638e 100644 --- a/library/think/Debug.php +++ b/library/think/Debug.php @@ -11,6 +11,13 @@ namespace think; +use think\Config; +use think\exception\ClassNotFoundException; +use think\Log; +use think\Request; +use think\Response; +use think\response\Redirect; + class Debug { // 区间时间信息 @@ -177,4 +184,43 @@ class Debug } } + public static function inject(Response $response) + { + $config = Config::get('trace'); + $type = isset($config['type']) ? $config['type'] : 'Html'; + + if (false !== $type) { + $request = Request::instance(); + $accept = $request->header('accept'); + $contentType = $response->getHeader('Content-Type'); + $class = false !== strpos($type, '\\') ? $type : '\\think\\debug\\' . ucwords($type); + unset($config['type']); + if (class_exists($class)) { + $trace = new $class($config); + } else { + throw new ClassNotFoundException('class not exists:' . $class, $class); + } + + if ($response instanceof Redirect) { + //TODO 记录 + } elseif (strpos($accept, 'application/json') === 0 || $request->isAjax()) { + //TODO 记录 + } elseif (!empty($contentType) && strpos($contentType, 'html') === false) { + //TODO 记录 + } else { + $output = $trace->output(Log::getLog()); + if (is_string($output)) { + // trace调试信息注入 + $content = $response->getContent(); + $pos = strripos($content, ''); + if (false !== $pos) { + $content = substr($content, 0, $pos) . $output . substr($content, $pos); + } else { + $content = $content . $output; + } + $response->content($content); + } + } + } + } } diff --git a/library/think/debug/trace/Console.php b/library/think/debug/Console.php similarity index 98% rename from library/think/debug/trace/Console.php rename to library/think/debug/Console.php index 0cbe12e6..205014ae 100644 --- a/library/think/debug/trace/Console.php +++ b/library/think/debug/Console.php @@ -9,7 +9,7 @@ // | Author: yangweijie // +---------------------------------------------------------------------- -namespace think\debug\trace; +namespace think\debug; use think\Cache; use think\Config; @@ -34,7 +34,7 @@ class Console } /** - * 日志写入接口 + * 调试输出接口 * @access public * @param array $log 日志信息 * @return bool diff --git a/library/think/debug/trace/Html.php b/library/think/debug/Html.php similarity index 98% rename from library/think/debug/trace/Html.php rename to library/think/debug/Html.php index 8e002567..0426fb41 100644 --- a/library/think/debug/trace/Html.php +++ b/library/think/debug/Html.php @@ -9,7 +9,7 @@ // | Author: liu21st // +---------------------------------------------------------------------- -namespace think\debug\trace; +namespace think\debug; use think\Cache; use think\Config; @@ -34,7 +34,7 @@ class Html } /** - * 日志写入接口 + * 调试输出接口 * @access public * @param array $log 日志信息 * @return bool diff --git a/library/think/log/driver/Socket.php b/library/think/debug/Socket.php similarity index 98% rename from library/think/log/driver/Socket.php rename to library/think/debug/Socket.php index 2d423a5d..9433dd09 100644 --- a/library/think/log/driver/Socket.php +++ b/library/think/debug/Socket.php @@ -9,7 +9,7 @@ // | Author: luofei614 // +---------------------------------------------------------------------- -namespace think\log\driver; +namespace think\debug; /** * github: https://github.com/luofei614/SocketLog @@ -55,12 +55,12 @@ class Socket } /** - * 日志写入接口 + * 调试输出接口 * @access public * @param array $logs 日志信息 * @return bool */ - public function save(array $logs = []) + public function output(array $logs = []) { if (!$this->check()) { return false; diff --git a/library/think/debug/Trace.php b/library/think/debug/Trace.php deleted file mode 100644 index f44b0b4f..00000000 --- a/library/think/debug/Trace.php +++ /dev/null @@ -1,63 +0,0 @@ - -// +---------------------------------------------------------------------- -namespace think\debug; - -use think\Config; -use think\exception\ClassNotFoundException; -use think\Log; -use think\Request; -use think\Response; -use think\response\Redirect; - -class Trace -{ - public static function inject(Response $response) - { - $config = Config::get('trace'); - - $type = isset($config['type']) ? $config['type'] : 'Html'; - - if ($type !== false) { - $request = Request::instance(); - $accept = $request->header('accept'); - $contentType = $response->getHeader('Content-Type'); - - $class = false !== strpos($type, '\\') ? $type : '\\think\\debug\\trace\\' . ucwords($type); - unset($config['type']); - if(class_exists($class)) { - $trace = new $class($config); - } else { - throw new ClassNotFoundException('class not exists:' . $class, $class); - } - - if ($response instanceof Redirect) { - //TODO 记录 - } elseif (strpos($accept, 'application/json') === 0 || $request->isAjax()) { - //TODO 记录 - } elseif (!empty($contentType) && strpos($contentType, 'html') === false) { - //TODO 记录 - } else { - $output = $trace->output(Log::getLog()); - - $content = $response->getContent(); - - $pos = strripos($content, ''); - if (false !== $pos) { - $content = substr($content, 0, $pos) . $output . substr($content, $pos); - } else { - $content = $content . $output; - } - - $response->content($content); - } - } - } -} \ No newline at end of file