mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
数据库和缓存读写计数调整 增加页面Trace日志驱动(需要设置'response_exit'=>false) Log::record方法支持字符串之外的变量输出
This commit is contained in:
@@ -13,6 +13,9 @@ namespace think;
|
||||
|
||||
class Cache
|
||||
{
|
||||
public static $readTimes = 0;
|
||||
public static $writeTimes = 0;
|
||||
|
||||
/**
|
||||
* 操作句柄
|
||||
* @var object
|
||||
|
||||
2
library/think/cache/driver/apc.php
vendored
2
library/think/cache/driver/apc.php
vendored
@@ -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'];
|
||||
}
|
||||
|
||||
2
library/think/cache/driver/db.php
vendored
2
library/think/cache/driver/db.php
vendored
@@ -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')) {
|
||||
|
||||
2
library/think/cache/driver/eaccelerator.php
vendored
2
library/think/cache/driver/eaccelerator.php
vendored
@@ -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'];
|
||||
}
|
||||
|
||||
2
library/think/cache/driver/file.php
vendored
2
library/think/cache/driver/file.php
vendored
@@ -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'];
|
||||
}
|
||||
|
||||
2
library/think/cache/driver/memcache.php
vendored
2
library/think/cache/driver/memcache.php
vendored
@@ -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'];
|
||||
}
|
||||
|
||||
2
library/think/cache/driver/redis.php
vendored
2
library/think/cache/driver/redis.php
vendored
@@ -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'];
|
||||
}
|
||||
|
||||
2
library/think/cache/driver/sae.php
vendored
2
library/think/cache/driver/sae.php
vendored
@@ -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'];
|
||||
}
|
||||
|
||||
2
library/think/cache/driver/secache.php
vendored
2
library/think/cache/driver/secache.php
vendored
@@ -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)) {
|
||||
|
||||
2
library/think/cache/driver/simple.php
vendored
2
library/think/cache/driver/simple.php
vendored
@@ -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);
|
||||
|
||||
2
library/think/cache/driver/sqlite.php
vendored
2
library/think/cache/driver/sqlite.php
vendored
@@ -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)) {
|
||||
|
||||
2
library/think/cache/driver/wincache.php
vendored
2
library/think/cache/driver/wincache.php
vendored
@@ -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'];
|
||||
}
|
||||
|
||||
2
library/think/cache/driver/xcache.php
vendored
2
library/think/cache/driver/xcache.php
vendored
@@ -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'];
|
||||
}
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 是否显示详细
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
95
library/think/log/driver/trace.php
Normal file
95
library/think/log/driver/trace.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user