mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 23:52:49 +08:00
简化slog类
This commit is contained in:
@@ -5,6 +5,8 @@
|
|||||||
*/
|
*/
|
||||||
namespace org;
|
namespace org;
|
||||||
|
|
||||||
|
use think\Exception;
|
||||||
|
|
||||||
class Slog
|
class Slog
|
||||||
{
|
{
|
||||||
public static $start_time = 0;
|
public static $start_time = 0;
|
||||||
@@ -12,8 +14,6 @@ class Slog
|
|||||||
public static $port = 1116; //SocketLog 服务的http的端口号
|
public static $port = 1116; //SocketLog 服务的http的端口号
|
||||||
public static $log_types = ['log', 'info', 'error', 'warn', 'table', 'group', 'groupCollapsed', 'groupEnd', 'alert'];
|
public static $log_types = ['log', 'info', 'error', 'warn', 'table', 'group', 'groupCollapsed', 'groupEnd', 'alert'];
|
||||||
|
|
||||||
protected static $_instance;
|
|
||||||
|
|
||||||
protected static $config = [
|
protected static $config = [
|
||||||
'enable' => true, //是否记录日志的开关
|
'enable' => true, //是否记录日志的开关
|
||||||
'host' => 'localhost',
|
'host' => 'localhost',
|
||||||
@@ -34,100 +34,12 @@ class Slog
|
|||||||
'sql_warn' => 'color:#009bb4;font-size:14px;',
|
'sql_warn' => 'color:#009bb4;font-size:14px;',
|
||||||
'error_handler' => 'color:#f4006b;font-size:14px;',
|
'error_handler' => 'color:#f4006b;font-size:14px;',
|
||||||
'page' => 'color:#40e2ff;background:#171717;',
|
'page' => 'color:#40e2ff;background:#171717;',
|
||||||
|
'big' => 'font-size:20px;color:red;',
|
||||||
];
|
];
|
||||||
|
|
||||||
public static function __callStatic($method, $args)
|
|
||||||
{
|
|
||||||
if (in_array($method, self::$log_types)) {
|
|
||||||
array_unshift($args, $method);
|
|
||||||
return call_user_func_array([self::getInstance(), 'record'], $args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function sql($sql, $link)
|
public static function sql($sql, $link)
|
||||||
{
|
{
|
||||||
if (is_object($link) && 'mysqli' == get_class($link)) {
|
if (is_object($link)) {
|
||||||
return self::mysqlilog($sql, $link);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_resource($link) && ('mysql link' == get_resource_type($link) || 'mysql link persistent' == get_resource_type($link))) {
|
|
||||||
return self::mysqllog($sql, $link);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_object($link) && 'PDO' == get_class($link)) {
|
|
||||||
return self::pdolog($sql, $link);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Exception('SocketLog can not support this database link');
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function big($log)
|
|
||||||
{
|
|
||||||
self::log($log, 'font-size:20px;color:red;');
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function trace($msg, $trace_level = 2, $css = '')
|
|
||||||
{
|
|
||||||
if (!self::check()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
self::groupCollapsed($msg, $css);
|
|
||||||
$traces = debug_backtrace(false);
|
|
||||||
$traces = array_reverse($traces);
|
|
||||||
$max = count($traces) - $trace_level;
|
|
||||||
for ($i = 0; $i < $max; $i++) {
|
|
||||||
$trace = $traces[$i];
|
|
||||||
$fun = isset($trace['class']) ? $trace['class'] . '::' . $trace['function'] : $trace['function'];
|
|
||||||
$file = isset($trace['file']) ? $trace['file'] : 'unknown file';
|
|
||||||
$line = isset($trace['line']) ? $trace['line'] : 'unknown line';
|
|
||||||
$trace_msg = '#' . $i . ' ' . $fun . ' called at [' . $file . ':' . $line . ']';
|
|
||||||
if (!empty($trace['args'])) {
|
|
||||||
self::groupCollapsed($trace_msg);
|
|
||||||
self::log($trace['args']);
|
|
||||||
self::groupEnd();
|
|
||||||
} else {
|
|
||||||
self::log($trace_msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self::groupEnd();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function mysqlilog($sql, $db)
|
|
||||||
{
|
|
||||||
if (!self::check()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$css = self::$css['sql'];
|
|
||||||
if (preg_match('/^SELECT /i', $sql)) {
|
|
||||||
//explain
|
|
||||||
$query = @mysqli_query($db, "EXPLAIN " . $sql);
|
|
||||||
$arr = mysqli_fetch_array($query);
|
|
||||||
self::sqlexplain($arr, $sql, $css);
|
|
||||||
}
|
|
||||||
self::sqlwhere($sql, $css);
|
|
||||||
self::trace($sql, 2, $css);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function mysqllog($sql, $db)
|
|
||||||
{
|
|
||||||
if (!self::check()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$css = self::$css['sql'];
|
|
||||||
if (preg_match('/^SELECT /i', $sql)) {
|
|
||||||
//explain
|
|
||||||
$query = @mysql_query("EXPLAIN " . $sql, $db);
|
|
||||||
$arr = mysql_fetch_array($query);
|
|
||||||
self::sqlexplain($arr, $sql, $css);
|
|
||||||
}
|
|
||||||
//判断sql语句是否有where
|
|
||||||
self::sqlwhere($sql, $css);
|
|
||||||
self::trace($sql, 2, $css);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function pdolog($sql, $pdo)
|
|
||||||
{
|
|
||||||
if (!self::check()) {
|
if (!self::check()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -148,6 +60,35 @@ class Slog
|
|||||||
self::trace($sql, 2, $css);
|
self::trace($sql, 2, $css);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new Exception('SocketLog can not support this database link');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function trace($msg, $trace_level = 2, $css = '')
|
||||||
|
{
|
||||||
|
if (!self::check()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self::groupCollapsed($msg, $css);
|
||||||
|
$traces = debug_backtrace(false);
|
||||||
|
$traces = array_reverse($traces);
|
||||||
|
$max = count($traces) - $trace_level;
|
||||||
|
for ($i = 0; $i < $max; $i++) {
|
||||||
|
$trace = $traces[$i];
|
||||||
|
$fun = isset($trace['class']) ? $trace['class'] . '::' . $trace['function'] : $trace['function'];
|
||||||
|
$file = isset($trace['file']) ? $trace['file'] : 'unknown file';
|
||||||
|
$line = isset($trace['line']) ? $trace['line'] : 'unknown line';
|
||||||
|
$trace_msg = '#' . $i . ' ' . $fun . ' called at [' . $file . ':' . $line . ']';
|
||||||
|
if (!empty($trace['args'])) {
|
||||||
|
self::record('groupCollapsed',$trace_msg);
|
||||||
|
self::record('log',$trace['args']);
|
||||||
|
self::record('groupEnd');
|
||||||
|
} else {
|
||||||
|
self::record('log',$trace_msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self::record('groupEnd');
|
||||||
|
}
|
||||||
|
|
||||||
private static function sqlexplain($arr, &$sql, &$css)
|
private static function sqlexplain($arr, &$sql, &$css)
|
||||||
{
|
{
|
||||||
$arr = array_change_key_case($arr, CASE_LOWER);
|
$arr = array_change_key_case($arr, CASE_LOWER);
|
||||||
@@ -240,23 +181,10 @@ class Slog
|
|||||||
// 保存日志记录
|
// 保存日志记录
|
||||||
if ($e = error_get_last()) {
|
if ($e = error_get_last()) {
|
||||||
self::error_handler($e['type'], $e['message'], $e['file'], $e['line']);
|
self::error_handler($e['type'], $e['message'], $e['file'], $e['line']);
|
||||||
self::sendLog(); //此类终止不会调用类的 __destruct 方法,所以此处手动sendLog
|
self::sendLog();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getInstance()
|
|
||||||
{
|
|
||||||
if (null === self::$_instance) {
|
|
||||||
self::$_instance = new self();
|
|
||||||
}
|
|
||||||
return self::$_instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static function _log($type, $logs, $css = '')
|
|
||||||
{
|
|
||||||
self::getInstance()->record($type, $logs, $css);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static function check()
|
protected static function check()
|
||||||
{
|
{
|
||||||
if (!self::getConfig('enable')) {
|
if (!self::getConfig('enable')) {
|
||||||
@@ -314,7 +242,6 @@ class Slog
|
|||||||
$config = array_merge(self::$config, $config);
|
$config = array_merge(self::$config, $config);
|
||||||
self::$config = $config;
|
self::$config = $config;
|
||||||
if (self::check()) {
|
if (self::check()) {
|
||||||
self::getInstance(); //强制初始化SocketLog实例
|
|
||||||
if ($config['optimize']) {
|
if ($config['optimize']) {
|
||||||
self::$start_time = microtime(true);
|
self::$start_time = microtime(true);
|
||||||
self::$start_memory = memory_get_usage();
|
self::$start_memory = memory_get_usage();
|
||||||
@@ -329,11 +256,7 @@ class Slog
|
|||||||
//获得配置
|
//获得配置
|
||||||
public static function getConfig($name)
|
public static function getConfig($name)
|
||||||
{
|
{
|
||||||
if (isset(self::$config[$name])) {
|
return isset(self::$config[$name]) ? self::$config[$name] : null;
|
||||||
return self::$config[$name];
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//记录日志
|
//记录日志
|
||||||
@@ -446,9 +369,12 @@ class Slog
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct()
|
public static function __callStatic($method, $args)
|
||||||
{
|
{
|
||||||
self::sendLog();
|
if (in_array($method, self::$log_types)) {
|
||||||
|
array_unshift($args, $method);
|
||||||
|
return call_user_func_array(self::record, $args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,10 +107,7 @@ class App
|
|||||||
// 操作方法执行完成监听
|
// 操作方法执行完成监听
|
||||||
Hook::listen('action_end', $data);
|
Hook::listen('action_end', $data);
|
||||||
// 返回数据
|
// 返回数据
|
||||||
Response::returnData($data, $config['default_return_type']);
|
Response::returnData($data, $config['default_return_type'], $config['response_exit']);
|
||||||
if ($config['response_exit']) {
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 操作方法不是Public 抛出异常
|
// 操作方法不是Public 抛出异常
|
||||||
throw new \ReflectionException();
|
throw new \ReflectionException();
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ class Error
|
|||||||
{
|
{
|
||||||
// 记录日志
|
// 记录日志
|
||||||
Log::save();
|
Log::save();
|
||||||
|
\org\Slog::sendLog();
|
||||||
if ($e = error_get_last()) {
|
if ($e = error_get_last()) {
|
||||||
switch ($e['type']) {
|
switch ($e['type']) {
|
||||||
case E_ERROR:
|
case E_ERROR:
|
||||||
|
|||||||
@@ -11,10 +11,6 @@
|
|||||||
|
|
||||||
namespace think;
|
namespace think;
|
||||||
|
|
||||||
use think\Config as Config;
|
|
||||||
use think\Transform as Transform;
|
|
||||||
use think\Url as Url;
|
|
||||||
|
|
||||||
class Response
|
class Response
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -23,9 +19,10 @@ class Response
|
|||||||
* @access protected
|
* @access protected
|
||||||
* @param mixed $data 要返回的数据
|
* @param mixed $data 要返回的数据
|
||||||
* @param String $type 返回数据格式
|
* @param String $type 返回数据格式
|
||||||
|
* @param bool $exit 是否终止执行
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function returnData($data, $type = '')
|
public static function returnData($data, $type = '', $exit = true)
|
||||||
{
|
{
|
||||||
$headers = [
|
$headers = [
|
||||||
'json' => 'application/json',
|
'json' => 'application/json',
|
||||||
@@ -55,8 +52,12 @@ class Response
|
|||||||
$data = $handler . '(' . Transform::jsonEncode($data) . ');';
|
$data = $handler . '(' . Transform::jsonEncode($data) . ');';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if ($exit) {
|
||||||
|
exit($data);
|
||||||
|
} else {
|
||||||
echo $data;
|
echo $data;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回封装后的API数据到客户端
|
* 返回封装后的API数据到客户端
|
||||||
@@ -73,7 +74,7 @@ class Response
|
|||||||
'code' => $code,
|
'code' => $code,
|
||||||
'msg' => $msg,
|
'msg' => $msg,
|
||||||
'time' => NOW_TIME,
|
'time' => NOW_TIME,
|
||||||
'data' => $data
|
'data' => $data,
|
||||||
];
|
];
|
||||||
self::returnData($result, $type);
|
self::returnData($result, $type);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class View
|
|||||||
|
|
||||||
public function __construct(array $config = [])
|
public function __construct(array $config = [])
|
||||||
{
|
{
|
||||||
$this->config(empty($config) ? Config::get() : $config);
|
$this->config($config);
|
||||||
$this->engine($this->config['engine_type']);
|
$this->engine($this->config['engine_type']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user