调试trace调试驱动 原来日志Socket驱动纳入trace功能

This commit is contained in:
thinkphp
2016-06-30 23:25:13 +08:00
parent 37fa2bceca
commit a61eda774f
7 changed files with 60 additions and 79 deletions

View File

@@ -140,7 +140,7 @@ return [
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
'log' => [ 'log' => [
// 日志记录方式,支持 file socket sae // 日志记录方式,支持 file sae
'type' => 'File', 'type' => 'File',
// 日志保存目录 // 日志保存目录
'path' => LOG_PATH, 'path' => LOG_PATH,
@@ -150,7 +150,7 @@ return [
// | Trace设置 // | Trace设置
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
'trace' => [ 'trace' => [
//支持Html,Console 设为false则不显示 // 支持Html Socket Console 设为false则不显示
'type' => false, 'type' => false,
], ],

View File

@@ -12,7 +12,6 @@
namespace think; namespace think;
use think\Config; use think\Config;
use think\debug\Trace;
use think\Exception; use think\Exception;
use think\exception\HttpException; use think\exception\HttpException;
use think\exception\HttpResponseException; use think\exception\HttpResponseException;
@@ -145,19 +144,18 @@ class App
$response = $data; $response = $data;
} elseif (!is_null($data)) { } elseif (!is_null($data)) {
// 默认自动识别响应输出类型 // 默认自动识别响应输出类型
$isAjax = $request->isAjax(); $isAjax = $request->isAjax();
$type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type'); $type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type');
$response = Response::create($data, $type); $response = Response::create($data, $type);
} else { } else {
$response = Response::create(); $response = Response::create();
} }
// 监听app_end // 监听app_end
Hook::listen('app_end', $response); Hook::listen('app_end', $response);
//注入Trace // Trace调试注入
self::$debug && Trace::inject($response); Debug::inject($response);
return $response; return $response;
} }

View File

@@ -11,6 +11,13 @@
namespace think; namespace think;
use think\Config;
use think\exception\ClassNotFoundException;
use think\Log;
use think\Request;
use think\Response;
use think\response\Redirect;
class Debug 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, '</body>');
if (false !== $pos) {
$content = substr($content, 0, $pos) . $output . substr($content, $pos);
} else {
$content = $content . $output;
}
$response->content($content);
}
}
}
}
} }

View File

@@ -9,7 +9,7 @@
// | Author: yangweijie <yangweijiester@gmail.com> // | Author: yangweijie <yangweijiester@gmail.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think\debug\trace; namespace think\debug;
use think\Cache; use think\Cache;
use think\Config; use think\Config;
@@ -34,7 +34,7 @@ class Console
} }
/** /**
* 日志写入接口 * 调试输出接口
* @access public * @access public
* @param array $log 日志信息 * @param array $log 日志信息
* @return bool * @return bool

View File

@@ -9,7 +9,7 @@
// | Author: liu21st <liu21st@gmail.com> // | Author: liu21st <liu21st@gmail.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think\debug\trace; namespace think\debug;
use think\Cache; use think\Cache;
use think\Config; use think\Config;
@@ -34,7 +34,7 @@ class Html
} }
/** /**
* 日志写入接口 * 调试输出接口
* @access public * @access public
* @param array $log 日志信息 * @param array $log 日志信息
* @return bool * @return bool

View File

@@ -9,7 +9,7 @@
// | Author: luofei614 <weibo.com/luofei614> // | Author: luofei614 <weibo.com/luofei614>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace think\log\driver; namespace think\debug;
/** /**
* github: https://github.com/luofei614/SocketLog * github: https://github.com/luofei614/SocketLog
@@ -55,12 +55,12 @@ class Socket
} }
/** /**
* 日志写入接口 * 调试输出接口
* @access public * @access public
* @param array $logs 日志信息 * @param array $logs 日志信息
* @return bool * @return bool
*/ */
public function save(array $logs = []) public function output(array $logs = [])
{ {
if (!$this->check()) { if (!$this->check()) {
return false; return false;

View File

@@ -1,63 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
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, '</body>');
if (false !== $pos) {
$content = substr($content, 0, $pos) . $output . substr($content, $pos);
} else {
$content = $content . $output;
}
$response->content($content);
}
}
}
}