diff --git a/library/think/cache.php b/library/think/cache.php index 650dbf26..abacae0e 100644 --- a/library/think/cache.php +++ b/library/think/cache.php @@ -13,6 +13,9 @@ namespace think; class Cache { + public static $readTimes = 0; + public static $writeTimes = 0; + /** * 操作句柄 * @var object diff --git a/library/think/cache/driver/apc.php b/library/think/cache/driver/apc.php index 554856e6..522b01c5 100644 --- a/library/think/cache/driver/apc.php +++ b/library/think/cache/driver/apc.php @@ -52,6 +52,7 @@ class Apc */ public function get($name) { + \think\Cache::$readTimes++; return apc_fetch($this->options['prefix'] . $name); } @@ -65,6 +66,7 @@ class Apc */ public function set($name, $value, $expire = null) { + \think\Cache::$writeTimes++; if (is_null($expire)) { $expire = $this->options['expire']; } diff --git a/library/think/cache/driver/db.php b/library/think/cache/driver/db.php index 11b12baf..39376828 100644 --- a/library/think/cache/driver/db.php +++ b/library/think/cache/driver/db.php @@ -55,6 +55,7 @@ class Db */ public function get($name) { + \think\Cache::$readTimes++; $name = $this->options['prefix'] . addslashes($name); $result = $this->handler->query('SELECT `data`,`datacrc` FROM `' . $this->options['table'] . '` WHERE `cachekey`=\'' . $name . '\' AND (`expire` =0 OR `expire`>' . time() . ') LIMIT 0,1'); if (false !== $result) { @@ -81,6 +82,7 @@ class Db */ public function set($name, $value, $expire = null) { + \think\Cache::$writeTimes++; $data = serialize($value); $name = $this->options['prefix'] . addslashes($name); if (function_exists('gzcompress')) { diff --git a/library/think/cache/driver/eaccelerator.php b/library/think/cache/driver/eaccelerator.php index c71141f8..8599fac5 100644 --- a/library/think/cache/driver/eaccelerator.php +++ b/library/think/cache/driver/eaccelerator.php @@ -44,6 +44,7 @@ class Eaccelerator */ public function get($name) { + \think\Cache::$readTimes++; return eaccelerator_get($this->options['prefix'] . $name); } @@ -57,6 +58,7 @@ class Eaccelerator */ public function set($name, $value, $expire = null) { + \think\Cache::$writeTimes++; if (is_null($expire)) { $expire = $this->options['expire']; } diff --git a/library/think/cache/driver/file.php b/library/think/cache/driver/file.php index d1fa67f0..de5515ed 100644 --- a/library/think/cache/driver/file.php +++ b/library/think/cache/driver/file.php @@ -98,6 +98,7 @@ class File if (!is_file($filename)) { return false; } + \think\Cache::$readTimes++; $content = file_get_contents($filename); if (false !== $content) { $expire = (int) substr($content, 8, 12); @@ -128,6 +129,7 @@ class File */ public function set($name, $value, $expire = null) { + \think\Cache::$writeTimes++; if (is_null($expire)) { $expire = $this->options['expire']; } diff --git a/library/think/cache/driver/memcache.php b/library/think/cache/driver/memcache.php index b037ee38..a49c8c79 100644 --- a/library/think/cache/driver/memcache.php +++ b/library/think/cache/driver/memcache.php @@ -63,6 +63,7 @@ class Memcache */ public function get($name) { + \think\Cache::$readTimes++; return $this->handler->get($this->options['prefix'] . $name); } @@ -76,6 +77,7 @@ class Memcache */ public function set($name, $value, $expire = null) { + \think\Cache::$writeTimes++; if (is_null($expire)) { $expire = $this->options['expire']; } diff --git a/library/think/cache/driver/redis.php b/library/think/cache/driver/redis.php index 39d297e3..cbf9c072 100644 --- a/library/think/cache/driver/redis.php +++ b/library/think/cache/driver/redis.php @@ -58,6 +58,7 @@ class Redis */ public function get($name) { + \think\Cache::$readTimes++; return $this->handler->get($this->options['prefix'] . $name); } @@ -71,6 +72,7 @@ class Redis */ public function set($name, $value, $expire = null) { + \think\Cache::$writeTimes++; if (is_null($expire)) { $expire = $this->options['expire']; } diff --git a/library/think/cache/driver/sae.php b/library/think/cache/driver/sae.php index bffe447f..445ae56d 100644 --- a/library/think/cache/driver/sae.php +++ b/library/think/cache/driver/sae.php @@ -56,6 +56,7 @@ class Sae */ public function get($name) { + \think\Cache::$readTimes++; return $this->handler->get($_SERVER['HTTP_APPVERSION'] . '/' . $this->options['prefix'] . $name); } @@ -69,6 +70,7 @@ class Sae */ public function set($name, $value, $expire = null) { + \think\Cache::$writeTimes++; if (is_null($expire)) { $expire = $this->options['expire']; } diff --git a/library/think/cache/driver/secache.php b/library/think/cache/driver/secache.php index 5c7f541b..1e086286 100644 --- a/library/think/cache/driver/secache.php +++ b/library/think/cache/driver/secache.php @@ -53,6 +53,7 @@ class Secache */ public function get($name) { + \think\Cache::$readTimes++; $name = $this->options['prefix'] . $name; $key = md5($name); $this->handler->fetch($key, $return); @@ -69,6 +70,7 @@ class Secache */ public function set($name, $value) { + \think\Cache::$writeTimes++; $name = $this->options['prefix'] . $name; $key = md5($name); if ($result = $this->handler->store($key, $value)) { diff --git a/library/think/cache/driver/simple.php b/library/think/cache/driver/simple.php index cd561072..40edcc9b 100644 --- a/library/think/cache/driver/simple.php +++ b/library/think/cache/driver/simple.php @@ -59,6 +59,7 @@ class Simple */ public function get($name) { + \think\Cache::$readTimes++; $filename = $this->filename($name); if (is_file($filename)) { return include $filename; @@ -79,6 +80,7 @@ class Simple */ public function set($name, $value) { + \think\Cache::$writeTimes++; $filename = $this->filename($name); // 缓存数据 $dir = dirname($filename); diff --git a/library/think/cache/driver/sqlite.php b/library/think/cache/driver/sqlite.php index e4de8367..46f672b8 100644 --- a/library/think/cache/driver/sqlite.php +++ b/library/think/cache/driver/sqlite.php @@ -57,6 +57,7 @@ class Sqlite */ public function get($name) { + \think\Cache::$readTimes++; $name = $this->options['prefix'] . sqlite_escape_string($name); $sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . time() . ') LIMIT 1'; $result = sqlite_query($this->handler, $sql); @@ -81,6 +82,7 @@ class Sqlite */ public function set($name, $value, $expire = null) { + \think\Cache::$writeTimes++; $name = $this->options['prefix'] . sqlite_escape_string($name); $value = sqlite_escape_string(serialize($value)); if (is_null($expire)) { diff --git a/library/think/cache/driver/wincache.php b/library/think/cache/driver/wincache.php index 99b5a87c..fdf46105 100644 --- a/library/think/cache/driver/wincache.php +++ b/library/think/cache/driver/wincache.php @@ -52,6 +52,7 @@ class Wincache */ public function get($name) { + \think\Cache::$readTimes++; $name = $this->options['prefix'] . $name; return wincache_ucache_exists($name) ? wincache_ucache_get($name) : false; } @@ -66,6 +67,7 @@ class Wincache */ public function set($name, $value, $expire = null) { + \think\Cache::$writeTimes++; if (is_null($expire)) { $expire = $this->options['expire']; } diff --git a/library/think/cache/driver/xcache.php b/library/think/cache/driver/xcache.php index f1f3fc0b..f1265d91 100644 --- a/library/think/cache/driver/xcache.php +++ b/library/think/cache/driver/xcache.php @@ -49,6 +49,7 @@ class Xcache */ public function get($name) { + \think\Cache::$readTimes++; $name = $this->options['prefix'] . $name; if (xcache_isset($name)) { return xcache_get($name); @@ -66,6 +67,7 @@ class Xcache */ public function set($name, $value, $expire = null) { + \think\Cache::$writeTimes++; if (is_null($expire)) { $expire = $this->options['expire']; } diff --git a/library/think/db.php b/library/think/db.php index 6ac35d63..75b5ba00 100644 --- a/library/think/db.php +++ b/library/think/db.php @@ -20,6 +20,10 @@ class Db private static $instance = []; // 当前数据库连接实例 private static $_instance = null; + // 查询次数 + public static $queryTimes = 0; + // 执行次数 + public static $executeTimes = 0; /** * 取得数据库类实例 @@ -55,8 +59,8 @@ class Db if (empty($config)) { $config = Config::get('database'); if (Config::get('use_db_switch')) { - $status = Config::get('app_status'); - $config = $config[$status?:'default']; + $status = Config::get('app_status'); + $config = $config[$status ?: 'default']; } } if (is_string($config)) { diff --git a/library/think/db/driver.php b/library/think/db/driver.php index ea169b35..70cebcd7 100644 --- a/library/think/db/driver.php +++ b/library/think/db/driver.php @@ -13,6 +13,7 @@ namespace think\db; use PDO; use think\Config; +use think\Db; use think\Debug; use think\Exception; use think\Log; @@ -61,10 +62,7 @@ abstract class Driver protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'notin' => 'NOT IN', 'not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN']; // 查询表达式 protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%LOCK%%COMMENT%'; - // 查询次数 - protected $queryTimes = 0; - // 执行次数 - protected $executeTimes = 0; + // PDO连接参数 protected $options = [ PDO::ATTR_CASE => PDO::CASE_LOWER, @@ -164,7 +162,7 @@ abstract class Driver $this->free(); } - $this->queryTimes++; + Db::$queryTimes++; // 调试开始 $this->debug(true); $this->PDOStatement = $this->_linkID->prepare($str); @@ -223,7 +221,7 @@ abstract class Driver $this->free(); } - $this->executeTimes++; + Db::$executeTimes++; // 记录开始执行时间 $this->debug(true); $this->PDOStatement = $this->_linkID->prepare($str); @@ -335,7 +333,7 @@ abstract class Driver */ public function getQueryTimes($execute = false) { - return $execute ? $this->queryTimes + $this->executeTimes : $this->queryTimes; + return $execute ? Db::$queryTimes + Db::$executeTimes : Db::$queryTimes; } /** @@ -345,7 +343,7 @@ abstract class Driver */ public function getExecuteTimes() { - return $this->executeTimes; + return Db::$executeTimes; } /** diff --git a/library/think/debug.php b/library/think/debug.php index 9ff563b1..55aea77b 100644 --- a/library/think/debug.php +++ b/library/think/debug.php @@ -129,15 +129,6 @@ class Debug return round($size, $dec) . " " . $a[$pos]; } - /** - * 获取数据库查询信息 - * @return void - */ - public static function getDbQuery() - { - return Db::getInstance()->getQueryTimes() . ' queries ' . Db::getInstance()->getExecuteTimes() . ' writes '; - } - /** * 获取文件加载信息 * @param bool $detail 是否显示详细 diff --git a/library/think/log.php b/library/think/log.php index ecf44dde..ef3ea625 100644 --- a/library/think/log.php +++ b/library/think/log.php @@ -64,6 +64,9 @@ class Log */ public static function record($msg, $type = 'log') { + if (!is_string($msg)) { + $msg = print_r($msg, true); + } self::$log[] = ['type' => $type, 'msg' => $msg]; } @@ -84,6 +87,9 @@ class Log */ public static function write($msg, $type) { + if (!is_string($msg)) { + $msg = print_r($msg, true); + } if ('error' == $type) { // 预留预警通知接口 self::$alarm && self::$alarm->send($msg); diff --git a/library/think/log/driver/socket.php b/library/think/log/driver/socket.php index eac26891..1c77b202 100644 --- a/library/think/log/driver/socket.php +++ b/library/think/log/driver/socket.php @@ -88,7 +88,7 @@ class Socket ]; foreach ($logs as &$log) { - if ('sql' == $log['type']) { + if (in_array($log['type'], ['sql', 'debug', 'info'])) { $log['type'] = 'log'; } } diff --git a/library/think/log/driver/trace.php b/library/think/log/driver/trace.php new file mode 100644 index 00000000..f91ac0d2 --- /dev/null +++ b/library/think/log/driver/trace.php @@ -0,0 +1,95 @@ + +// +---------------------------------------------------------------------- +namespace think\log\driver; + +/** + * 页面Trace调试 需要设置 'response_exit' => false 才能生效 + */ +class Trace +{ + protected $tabs = ['base' => '基本', 'file' => '文件', 'warn|error' => '错误', 'sql' => 'SQL', 'info|debug|log' => '调试']; + protected $config = [ + 'trace_file' => '', + ]; + + // 实例化并传入参数 + public function __construct($config = []) + { + $this->config['trace_file'] = THINK_PATH . 'tpl/page_trace.tpl'; + $this->config = array_merge($this->config, $config); + } + + /** + * 日志写入接口 + * @access public + * @param array $log 日志信息 + * @return void + */ + public function save($log = []) + { + + // 获取基本信息 + $current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; + $runtime = number_format(microtime(true) - START_TIME, 6); + $reqs = number_format(1 / $runtime, 2); + + // 页面Trace信息 + $base = [ + '请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $current_uri, + '运行时间' => "{$runtime}s [ 吞吐率:{$reqs}req/s ]", + '内存消耗' => number_format((memory_get_usage() - START_MEM) / 1024, 2) . 'kb', + '查询信息' => \think\Db::$queryTimes . ' queries ' . \think\Db::$executeTimes . ' writes ', + '缓存信息' => \think\Cache::$readTimes . ' reads,' . \think\Cache::$writeTimes . ' writes', + '文件加载' => count(get_included_files()), + '配置加载' => count(\think\Config::get()), + '会话信息' => 'SESSION_ID=' . session_id(), + ]; + + $info = \think\Debug::getFile(true); + + // 获取调试日志 + $debug = []; + foreach ($log as $line) { + $debug[$line['type']][] = $line['msg']; + } + + // 页面Trace信息 + $trace = []; + foreach ($this->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] : ''; + } + } + } + // 调用Trace页面模板 + ob_start(); + include $this->config['trace_file']; + echo ob_get_clean(); + } + +} diff --git a/library/think/response.php b/library/think/response.php index 9ae3c0b2..7f688193 100644 --- a/library/think/response.php +++ b/library/think/response.php @@ -52,7 +52,7 @@ class Response $data = $handler . '(' . \org\Transform::jsonEncode($data) . ');'; break; } - header('Content-Length:' . strlen($data)); + //header('Content-Length:' . strlen($data)); if ($exit) { exit($data); } else { diff --git a/tpl/page_trace.tpl b/tpl/page_trace.tpl index caa4c570..4b9f7ed9 100644 --- a/tpl/page_trace.tpl +++ b/tpl/page_trace.tpl @@ -23,7 +23,7 @@ -
+