From 67a6f398c6b1210ce7111c2881f97c36864c3a1b Mon Sep 17 00:00:00 2001 From: jay <917647288@qq.com> Date: Sun, 12 Jun 2016 08:53:21 +0800 Subject: [PATCH 1/3] add Browser console log support --- library/think/log/driver/Browser.php | 78 ++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 library/think/log/driver/Browser.php diff --git a/library/think/log/driver/Browser.php b/library/think/log/driver/Browser.php new file mode 100644 index 00000000..1f8597de --- /dev/null +++ b/library/think/log/driver/Browser.php @@ -0,0 +1,78 @@ + 'File', + ]; + + // 实例化并传入参数 + public function __construct($config = []) + { + if (is_array($config)) { + $this->config = array_merge($this->config, $config); + } + } + + /** + * 日志写入接口 + * @access public + * @param array $log 日志信息 + * @return bool + */ + public function save(array $log = []) + { + $request = Request::instance(); + $type = $request->type(); + $type = config('default_return_type'); + //输出到控制台 + if(in_array($type, ['html', 'txt'])){ + $lines = []; + foreach ($log as $key => $l) { + $lines[] = $this->output($l['type'], $l['msg']); + } + $lines = implode(PHP_EOL, $lines); + $js = << +{$lines} + +JS; + echo $js; + }else{ + $other_save = $this->config['notview_save']; + // $other_save = "\think\log\driver\"".$other_save; + $other_save = 'think\log\driver\\'.$other_save; + $other_save_class = new $other_save(); + $other_save_class->save($log); + } + return true; + } + + public function output($type, $msg){ + // dump($type); + // dump($msg); + $msg = str_replace(PHP_EOL, '\n', $msg); + if(in_array($type, ['info', 'log', 'error', 'warn', 'debug'])){ + $style = ''; + if('error' == $type){ + $style = 'color:#F4006B;font-size:14px;'; + } + $line = "console.{$type}(\"%c{$msg}\", \"{$style}\");"; + }else{ + if('sql' == $type){ + $style = "color:#009bb4;"; + $line = "console.log(\"%c{$msg}\", \"{$style}\");"; + }else{ + $line = "alert(\"{$msg}\");"; + } + } + return $line; + } + +} From 1914fcad4daf2ca3c806b02fbb4883b136709901 Mon Sep 17 00:00:00 2001 From: jay <917647288@qq.com> Date: Tue, 14 Jun 2016 00:15:03 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BA=E9=80=82?= =?UTF-8?q?=E5=90=88=E6=9C=80=E6=96=B0=E7=89=88=E6=9C=AC=E7=9A=84=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E8=BE=93=E5=87=BA=20Browser=20trace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 支持数组的前端显示,对象的有问题。采用分组模式 --- library/think/log/driver/Browser.php | 140 ++++++++++++++++++++------- 1 file changed, 104 insertions(+), 36 deletions(-) diff --git a/library/think/log/driver/Browser.php b/library/think/log/driver/Browser.php index 1f8597de..a86fb142 100644 --- a/library/think/log/driver/Browser.php +++ b/library/think/log/driver/Browser.php @@ -1,14 +1,18 @@ 'File', + 'trace_tabs' => ['base' => '基本', 'file' => '文件', 'info' => '流程', 'notice|error' => '错误', 'sql' => 'SQL', 'debug|log' => '调试'], ]; // 实例化并传入参数 @@ -27,52 +31,116 @@ class Browser */ public function save(array $log = []) { - $request = Request::instance(); - $type = $request->type(); - $type = config('default_return_type'); - //输出到控制台 - if(in_array($type, ['html', 'txt'])){ - $lines = []; - foreach ($log as $key => $l) { - $lines[] = $this->output($l['type'], $l['msg']); + if (IS_CLI || IS_API || Request::instance()->isAjax() || (defined('RESPONSE_TYPE') && !in_array(RESPONSE_TYPE, ['html', 'view']))) { + // ajax cli api方式下不输出 + return false; + } + // 获取基本信息 + $runtime = microtime(true) - START_TIME; + $reqs = number_format(1 / number_format($runtime, 8), 2); + $runtime = number_format($runtime, 6); + $mem = number_format((memory_get_usage() - START_MEM) / 1024, 2); + + // 页面Trace信息 + $base = [ + '请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], + '运行时间' => "{$runtime}s [ 吞吐率:{$reqs}req/s ] 内存消耗:{$mem}kb 文件加载:" . count(get_included_files()), + '查询信息' => Db::$queryTimes . ' queries ' . Db::$executeTimes . ' writes ', + '缓存信息' => Cache::$readTimes . ' reads,' . Cache::$writeTimes . ' writes', + '配置加载' => count(Config::get()), + ]; + + if (session_id()) { + $base['会话信息'] = 'SESSION_ID=' . session_id(); + } + + $info = Debug::getFile(true); + + // 获取调试日志 + $debug = []; + foreach ($log as $type => $val) { + foreach ($val as $msg) { + $debug[$type][] = $msg; } - $lines = implode(PHP_EOL, $lines); - $js = <<config['trace_tabs'] as $name => $title) { + $name = strtolower($name); + switch ($name) { + case 'base': // 基本信息 + $trace[$title] = $base; + break; + case 'file': // 文件信息 + $trace[$title] = $info; + break; + default: // 调试信息 + if (strpos($name, '|')) { + // 多组信息 + $names = explode('|', $name); + $result = []; + foreach ($names as $name) { + $result = array_merge($result, isset($debug[$name]) ? $debug[$name] : []); + } + $trace[$title] = $result; + } else { + $trace[$title] = isset($debug[$name]) ? $debug[$name] : ''; + } + } + } + + //输出到控制台 + $lines = ''; + foreach ($trace as $type => $msg) { + $lines .= $this->output($type, $msg); + } + $js = << {$lines} JS; - echo $js; - }else{ - $other_save = $this->config['notview_save']; - // $other_save = "\think\log\driver\"".$other_save; - $other_save = 'think\log\driver\\'.$other_save; - $other_save_class = new $other_save(); - $other_save_class->save($log); - } + echo $js; + return true; } public function output($type, $msg){ - // dump($type); - // dump($msg); - $msg = str_replace(PHP_EOL, '\n', $msg); - if(in_array($type, ['info', 'log', 'error', 'warn', 'debug'])){ - $style = ''; - if('error' == $type){ - $style = 'color:#F4006B;font-size:14px;'; - } - $line = "console.{$type}(\"%c{$msg}\", \"{$style}\");"; - }else{ - if('sql' == $type){ - $style = "color:#009bb4;"; - $line = "console.log(\"%c{$msg}\", \"{$style}\");"; - }else{ - $line = "alert(\"{$msg}\");"; + $type = strtolower($type); + $line[] = "console.group('{$type}');"; + foreach ($msg as $key => $m) { + switch ($type) { + case '调试': + //我多么希望进来的是原数据格式而不是字符串 + if(substr($m, 0, 5) == 'array'){ + eval("\$o = $m;"); + $line[] = "console.log(".json_encode($o).");"; + }else{ + $msg = addslashes($m); + $msg = str_replace(PHP_EOL, '\n', $msg); + $line[] = "console.log('$msg');"; + } + break; + case 'error': + $msg = str_replace(PHP_EOL, '\n', $m); + $style = 'color:#F4006B;font-size:14px;'; + $line[] = "console.log(\"%c{$msg}\", \"{$style}\");"; + break; + case 'sql': + $msg = str_replace(PHP_EOL, '\n', $m); + $style = "color:#009bb4;"; + $line[] = "console.log(\"%c{$msg}\", \"{$style}\");"; + break; + default: + $m = is_string($key)? $key.' '.$m: $key+1 .' '.$m; + $msg = str_replace(PHP_EOL, '\n', $m); + $line[] = "console.log(\"{$msg}\");"; + break; } } - return $line; + $line[]= "console.groupEnd();"; + return implode(PHP_EOL, $line); } } From 4993f4a0cb5984f63d1a8733ee1dca9d8c5e9ceb Mon Sep 17 00:00:00 2001 From: jay <917647288@qq.com> Date: Tue, 14 Jun 2016 08:44:55 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=88=86=E7=BB=84=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=B1=95=E5=BC=80=E5=9F=BA=E6=9C=AC=E5=92=8C=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E5=92=8C=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/log/driver/Browser.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/library/think/log/driver/Browser.php b/library/think/log/driver/Browser.php index a86fb142..66106846 100644 --- a/library/think/log/driver/Browser.php +++ b/library/think/log/driver/Browser.php @@ -108,7 +108,11 @@ JS; public function output($type, $msg){ $type = strtolower($type); - $line[] = "console.group('{$type}');"; + $trace_tabs = array_values($this->config['trace_tabs']); + $line[] = ($type == $trace_tabs[0] || '调试' == $type || '错误'== $type)? + "console.group('{$type}');" + : + "console.groupCollapsed('{$type}');"; foreach ($msg as $key => $m) { switch ($type) { case '调试': @@ -122,10 +126,11 @@ JS; $line[] = "console.log('$msg');"; } break; - case 'error': + case '错误': $msg = str_replace(PHP_EOL, '\n', $m); $style = 'color:#F4006B;font-size:14px;'; - $line[] = "console.log(\"%c{$msg}\", \"{$style}\");"; + $line[] = "console.error(\"%c{$msg}\", \"{$style}\");"; + // $line[] = "console.error(".json_encode(debug_backtrace()).");"; break; case 'sql': $msg = str_replace(PHP_EOL, '\n', $m);