From fbf9e938e8e1d7f1d3c9c6a951124db66fa9026c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 3 Jul 2016 10:39:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0app=5Ftrace=20=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E7=94=A8=E4=BA=8E=E9=85=8D=E7=BD=AE=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=BC=80=E5=90=AFtrace=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convention.php | 13 ++++++--- library/think/App.php | 4 ++- library/think/Debug.php | 61 ++++++++++++++++++++--------------------- library/think/Error.php | 20 ++++++-------- library/think/Log.php | 10 +++---- 5 files changed, 54 insertions(+), 54 deletions(-) diff --git a/convention.php b/convention.php index efcf27fc..00268b48 100644 --- a/convention.php +++ b/convention.php @@ -9,6 +9,8 @@ return [ 'app_namespace' => 'app', // 应用调试模式 'app_debug' => true, + // 应用Trace + 'app_trace' => false, // 应用模式状态 'app_status' => '', // 是否支持多模块 @@ -134,24 +136,26 @@ return [ 'error_message' => '页面错误!请稍后再试~', // 显示错误信息 'show_error_msg' => false, + // 异常处理handle类 留空使用 \think\exception\Handle + 'exception_handle' => '', // +---------------------------------------------------------------------- // | 日志设置 // +---------------------------------------------------------------------- 'log' => [ - // 日志记录方式,支持 file sae + // 日志记录方式,内置 file sae 支持扩展 'type' => 'File', // 日志保存目录 'path' => LOG_PATH, ], // +---------------------------------------------------------------------- - // | Trace设置 + // | Trace设置 开启 app_trace 后 有效 // +---------------------------------------------------------------------- 'trace' => [ - // 支持Html Socket Console 设为false则不显示 - 'type' => false, + // 内置Html Socket Console 支持扩展 + 'type' => 'Html', ], // +---------------------------------------------------------------------- @@ -245,6 +249,7 @@ return [ // 自动写入时间戳字段 'auto_timestamp' => false, ], + //分页配置 'paginate' => [ 'type' => 'bootstrap', diff --git a/library/think/App.php b/library/think/App.php index b6438d04..9226284e 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -155,7 +155,9 @@ class App Hook::listen('app_end', $response); // Trace调试注入 - Debug::inject($response); + if (Config::get('app_trace')) { + Debug::inject($response); + } return $response; } diff --git a/library/think/Debug.php b/library/think/Debug.php index 05695400..8dee74fe 100644 --- a/library/think/Debug.php +++ b/library/think/Debug.php @@ -186,40 +186,37 @@ class Debug public static function inject(Response $response) { - $config = Config::get('trace'); - $type = isset($config['type']) ? $config['type'] : 'Html'; + $config = Config::get('trace'); + $type = isset($config['type']) ? $config['type'] : 'Html'; + $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 (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); + 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/Error.php b/library/think/Error.php index 047b12d6..28f75882 100644 --- a/library/think/Error.php +++ b/library/think/Error.php @@ -63,7 +63,7 @@ class Error if (error_reporting() & $errno) { // 将错误信息托管至 think\exception\ErrorException throw $exception; - }else{ + } else { self::getExceptionHandler()->report($exception); } } @@ -98,24 +98,20 @@ class Error /** * Get an instance of the exception handler. * - * @return \think\exception\Handle + * @return Handle */ public static function getExceptionHandler() { static $handle; - if (!$handle) { - - if ($class = Config::get('exception_handle')) { - if (class_exists($class) && is_subclass_of($class, "\\think\\exception\\Handle")) { - $handle = new $class; - } - } - if (!$handle) { - $handle = new Handle(); + // 异常处理handle + $class = Config::get('exception_handle'); + if ($class && class_exists($class) && is_subclass_of($class, "\\think\\exception\\Handle")) { + $handle = new $class; + } else { + $handle = new Handle; } } - return $handle; } } diff --git a/library/think/Log.php b/library/think/Log.php index 608910a0..1db7dc13 100644 --- a/library/think/Log.php +++ b/library/think/Log.php @@ -10,12 +10,13 @@ // +---------------------------------------------------------------------- namespace think; + use think\exception\ClassNotFoundException; /** * Class Log * @package think - * + * * @method void log($msg) static * @method void error($msg) static * @method void info($msg) static @@ -48,13 +49,13 @@ class Log * 日志初始化 * @param array $config */ - public static function init($config = []) + public static function init($config = []) { $type = isset($config['type']) ? $config['type'] : 'File'; $class = false !== strpos($type, '\\') ? $type : '\\think\\log\\driver\\' . ucwords($type); self::$config = $config; unset($config['type']); - if(class_exists($class)) { + if (class_exists($class)) { self::$driver = new $class($config); } else { throw new ClassNotFoundException('class not exists:' . $class, $class); @@ -62,7 +63,7 @@ class Log // 记录初始化信息 App::$debug && Log::record('[ LOG ] INIT ' . $type . ': ' . var_export($config, true), 'info'); } - + /** * 获取日志信息 * @param string $type 信息类型 @@ -110,7 +111,6 @@ class Log */ public static function check($config) { - if (self::$key && !empty($config['allow_key']) && !in_array(self::$key, $config['allow_key'])) { return false; }