feat: 本次提交更新内容如下
php有问题,覆盖下
This commit is contained in:
@@ -12,7 +12,6 @@
|
||||
namespace think\log\driver;
|
||||
|
||||
use think\App;
|
||||
use think\Request;
|
||||
|
||||
/**
|
||||
* 本地化调试输出到文件
|
||||
@@ -20,27 +19,37 @@ use think\Request;
|
||||
class File
|
||||
{
|
||||
protected $config = [
|
||||
'time_format' => ' c ',
|
||||
'time_format' => 'c',
|
||||
'single' => false,
|
||||
'file_size' => 2097152,
|
||||
'path' => LOG_PATH,
|
||||
'path' => '',
|
||||
'apart_level' => [],
|
||||
'max_files' => 0,
|
||||
'json' => false,
|
||||
];
|
||||
|
||||
protected $app;
|
||||
|
||||
// 实例化并传入参数
|
||||
public function __construct($config = [])
|
||||
public function __construct(App $app, $config = [])
|
||||
{
|
||||
$this->app = $app;
|
||||
|
||||
if (is_array($config)) {
|
||||
$this->config = array_merge($this->config, $config);
|
||||
}
|
||||
|
||||
if (empty($this->config['path'])) {
|
||||
$this->config['path'] = $this->app->getRuntimePath() . 'log' . DIRECTORY_SEPARATOR;
|
||||
} elseif (substr($this->config['path'], -1) != DIRECTORY_SEPARATOR) {
|
||||
$this->config['path'] .= DIRECTORY_SEPARATOR;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 日志写入接口
|
||||
* @access public
|
||||
* @param array $log 日志信息
|
||||
* @param array $log 日志信息
|
||||
* @param bool $append 是否追加请求信息
|
||||
* @return bool
|
||||
*/
|
||||
@@ -52,6 +61,7 @@ class File
|
||||
!is_dir($path) && mkdir($path, 0755, true);
|
||||
|
||||
$info = [];
|
||||
|
||||
foreach ($log as $type => $val) {
|
||||
|
||||
foreach ($val as $msg) {
|
||||
@@ -67,6 +77,7 @@ class File
|
||||
$filename = $this->getApartLevelFile($path, $type);
|
||||
|
||||
$this->write($info[$type], $filename, true, $append);
|
||||
|
||||
unset($info[$type]);
|
||||
}
|
||||
}
|
||||
@@ -78,6 +89,45 @@ class File
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 日志写入
|
||||
* @access protected
|
||||
* @param array $message 日志信息
|
||||
* @param string $destination 日志文件
|
||||
* @param bool $apart 是否独立文件写入
|
||||
* @param bool $append 是否追加请求信息
|
||||
* @return bool
|
||||
*/
|
||||
protected function write($message, $destination, $apart = false, $append = false)
|
||||
{
|
||||
// 检测日志文件大小,超过配置大小则备份日志文件重新生成
|
||||
$this->checkLogSize($destination);
|
||||
|
||||
// 日志信息封装
|
||||
$info['timestamp'] = date($this->config['time_format']);
|
||||
|
||||
foreach ($message as $type => $msg) {
|
||||
$msg = is_array($msg) ? implode(PHP_EOL, $msg) : $msg;
|
||||
if (PHP_SAPI == 'cli') {
|
||||
$info['msg'] = $msg;
|
||||
$info['type'] = $type;
|
||||
} else {
|
||||
$info[$type] = $msg;
|
||||
}
|
||||
}
|
||||
|
||||
if (PHP_SAPI == 'cli') {
|
||||
$message = $this->parseCliLog($info);
|
||||
} else {
|
||||
// 添加调试日志
|
||||
$this->getDebugLog($info, $append, $apart);
|
||||
|
||||
$message = $this->parseLog($info);
|
||||
}
|
||||
|
||||
return error_log($message, 3, $destination);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主日志文件名
|
||||
* @access public
|
||||
@@ -85,21 +135,26 @@ class File
|
||||
*/
|
||||
protected function getMasterLogFile()
|
||||
{
|
||||
if ($this->config['max_files']) {
|
||||
$files = glob($this->config['path'] . '*.log');
|
||||
|
||||
try {
|
||||
if (count($files) > $this->config['max_files']) {
|
||||
unlink($files[0]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
}
|
||||
}
|
||||
|
||||
$cli = PHP_SAPI == 'cli' ? '_cli' : '';
|
||||
|
||||
if ($this->config['single']) {
|
||||
$name = is_string($this->config['single']) ? $this->config['single'] : 'single';
|
||||
$name = is_string($this->config['single']) ? $this->config['single'] : 'single';
|
||||
|
||||
$destination = $this->config['path'] . $name . $cli . '.log';
|
||||
} else {
|
||||
if ($this->config['max_files']) {
|
||||
$filename = date('Ymd') . $cli . '.log';
|
||||
$files = glob($this->config['path'] . '*.log');
|
||||
|
||||
try {
|
||||
if (count($files) > $this->config['max_files']) {
|
||||
unlink($files[0]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
}
|
||||
} else {
|
||||
$filename = date('Ym') . DIRECTORY_SEPARATOR . date('d') . $cli . '.log';
|
||||
}
|
||||
@@ -132,45 +187,6 @@ class File
|
||||
return $path . DIRECTORY_SEPARATOR . $name . '_' . $type . $cli . '.log';
|
||||
}
|
||||
|
||||
/**
|
||||
* 日志写入
|
||||
* @access protected
|
||||
* @param array $message 日志信息
|
||||
* @param string $destination 日志文件
|
||||
* @param bool $apart 是否独立文件写入
|
||||
* @param bool $append 是否追加请求信息
|
||||
* @return bool
|
||||
*/
|
||||
protected function write($message, $destination, $apart = false, $append = false)
|
||||
{
|
||||
// 检测日志文件大小,超过配置大小则备份日志文件重新生成
|
||||
$this->checkLogSize($destination);
|
||||
|
||||
// 日志信息封装
|
||||
$info['timestamp'] = date($this->config['time_format']);
|
||||
|
||||
foreach ($message as $type => $msg) {
|
||||
$msg = is_array($msg) ? implode("\r\n", $msg) : $msg;
|
||||
if (PHP_SAPI == 'cli') {
|
||||
$info['msg'] = $msg;
|
||||
$info['type'] = $type;
|
||||
} else {
|
||||
$info[$type] = $msg;
|
||||
}
|
||||
}
|
||||
|
||||
if (PHP_SAPI == 'cli') {
|
||||
$message = $this->parseCliLog($info);
|
||||
} else {
|
||||
// 添加调试日志
|
||||
$this->getDebugLog($info, $append, $apart);
|
||||
|
||||
$message = $this->parseLog($info);
|
||||
}
|
||||
|
||||
return error_log($message, 3, $destination);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查日志文件大小并自动生成备份文件
|
||||
* @access protected
|
||||
@@ -196,14 +212,14 @@ class File
|
||||
protected function parseCliLog($info)
|
||||
{
|
||||
if ($this->config['json']) {
|
||||
$message = json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\r\n";
|
||||
$message = json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
|
||||
} else {
|
||||
$now = $info['timestamp'];
|
||||
unset($info['timestamp']);
|
||||
|
||||
$message = implode("\r\n", $info);
|
||||
$message = implode(PHP_EOL, $info);
|
||||
|
||||
$message = "[{$now}]" . $message . "\r\n";
|
||||
$message = "[{$now}]" . $message . PHP_EOL;
|
||||
}
|
||||
|
||||
return $message;
|
||||
@@ -217,35 +233,34 @@ class File
|
||||
*/
|
||||
protected function parseLog($info)
|
||||
{
|
||||
$request = Request::instance();
|
||||
$requestInfo = [
|
||||
'ip' => $request->ip(),
|
||||
'method' => $request->method(),
|
||||
'host' => $request->host(),
|
||||
'uri' => $request->url(),
|
||||
'ip' => $this->app['request']->ip(),
|
||||
'method' => $this->app['request']->method(),
|
||||
'host' => $this->app['request']->host(),
|
||||
'uri' => $this->app['request']->url(),
|
||||
];
|
||||
|
||||
if ($this->config['json']) {
|
||||
$info = $requestInfo + $info;
|
||||
return json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\r\n";
|
||||
return json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
|
||||
}
|
||||
|
||||
array_unshift($info, "---------------------------------------------------------------\r\n[{$info['timestamp']}] {$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}");
|
||||
array_unshift($info, "---------------------------------------------------------------" . PHP_EOL . "\r\n[{$info['timestamp']}] {$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}");
|
||||
unset($info['timestamp']);
|
||||
|
||||
return implode("\r\n", $info) . "\r\n";
|
||||
return implode(PHP_EOL, $info) . PHP_EOL;
|
||||
}
|
||||
|
||||
protected function getDebugLog(&$info, $append, $apart)
|
||||
{
|
||||
if (App::$debug && $append) {
|
||||
if ($this->app->isDebug() && $append) {
|
||||
|
||||
if ($this->config['json']) {
|
||||
// 获取基本信息
|
||||
$runtime = round(microtime(true) - THINK_START_TIME, 10);
|
||||
$runtime = round(microtime(true) - $this->app->getBeginTime(), 10);
|
||||
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
|
||||
|
||||
$memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
|
||||
$memory_use = number_format((memory_get_usage() - $this->app->getBeginMem()) / 1024, 2);
|
||||
|
||||
$info = [
|
||||
'runtime' => number_format($runtime, 6) . 's',
|
||||
@@ -256,10 +271,10 @@ class File
|
||||
|
||||
} elseif (!$apart) {
|
||||
// 增加额外的调试信息
|
||||
$runtime = round(microtime(true) - THINK_START_TIME, 10);
|
||||
$runtime = round(microtime(true) - $this->app->getBeginTime(), 10);
|
||||
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
|
||||
|
||||
$memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
|
||||
$memory_use = number_format((memory_get_usage() - $this->app->getBeginMem()) / 1024, 2);
|
||||
|
||||
$time_str = '[运行时间:' . number_format($runtime, 6) . 's] [吞吐率:' . $reqs . 'req/s]';
|
||||
$memory_str = ' [内存消耗:' . $memory_use . 'kb]';
|
||||
|
||||
@@ -30,6 +30,8 @@ class Socket
|
||||
'force_client_ids' => [],
|
||||
// 限制允许读取日志的client_id
|
||||
'allow_client_ids' => [],
|
||||
//输出到浏览器默认展开的日志级别
|
||||
'expand_level' => ['debug'],
|
||||
];
|
||||
|
||||
protected $css = [
|
||||
@@ -41,14 +43,17 @@ class Socket
|
||||
];
|
||||
|
||||
protected $allowForceClientIds = []; //配置强制推送且被授权的client_id
|
||||
protected $app;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param array $config 缓存参数
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param array $config 缓存参数
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
public function __construct(App $app, array $config = [])
|
||||
{
|
||||
$this->app = $app;
|
||||
|
||||
if (!empty($config)) {
|
||||
$this->config = array_merge($this->config, $config);
|
||||
}
|
||||
@@ -57,7 +62,7 @@ class Socket
|
||||
/**
|
||||
* 调试输出接口
|
||||
* @access public
|
||||
* @param array $log 日志信息
|
||||
* @param array $log 日志信息
|
||||
* @return bool
|
||||
*/
|
||||
public function save(array $log = [], $append = false)
|
||||
@@ -65,12 +70,14 @@ class Socket
|
||||
if (!$this->check()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$trace = [];
|
||||
if (App::$debug) {
|
||||
$runtime = round(microtime(true) - THINK_START_TIME, 10);
|
||||
|
||||
if ($this->app->isDebug()) {
|
||||
$runtime = round(microtime(true) - $this->app->getBeginTime(), 10);
|
||||
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
|
||||
$time_str = ' [运行时间:' . number_format($runtime, 6) . 's][吞吐率:' . $reqs . 'req/s]';
|
||||
$memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
|
||||
$memory_use = number_format((memory_get_usage() - $this->app->getBeginMem()) / 1024, 2);
|
||||
$memory_str = ' [内存消耗:' . $memory_use . 'kb]';
|
||||
$file_load = ' [文件加载:' . count(get_included_files()) . ']';
|
||||
|
||||
@@ -79,6 +86,7 @@ class Socket
|
||||
} else {
|
||||
$current_uri = 'cmd:' . implode(' ', $_SERVER['argv']);
|
||||
}
|
||||
|
||||
// 基本信息
|
||||
$trace[] = [
|
||||
'type' => 'group',
|
||||
@@ -89,10 +97,11 @@ class Socket
|
||||
|
||||
foreach ($log as $type => $val) {
|
||||
$trace[] = [
|
||||
'type' => 'groupCollapsed',
|
||||
'type' => in_array($type, $this->config['expand_level']) ? 'group' : 'groupCollapsed',
|
||||
'msg' => '[ ' . $type . ' ]',
|
||||
'css' => isset($this->css[$type]) ? $this->css[$type] : '',
|
||||
];
|
||||
|
||||
foreach ($val as $msg) {
|
||||
if (!is_string($msg)) {
|
||||
$msg = var_export($msg, true);
|
||||
@@ -103,6 +112,7 @@ class Socket
|
||||
'css' => '',
|
||||
];
|
||||
}
|
||||
|
||||
$trace[] = [
|
||||
'type' => 'groupEnd',
|
||||
'msg' => '',
|
||||
@@ -116,11 +126,13 @@ class Socket
|
||||
'msg' => '[ file ]',
|
||||
'css' => '',
|
||||
];
|
||||
|
||||
$trace[] = [
|
||||
'type' => 'log',
|
||||
'msg' => implode("\n", get_included_files()),
|
||||
'css' => '',
|
||||
];
|
||||
|
||||
$trace[] = [
|
||||
'type' => 'groupEnd',
|
||||
'msg' => '',
|
||||
@@ -135,6 +147,7 @@ class Socket
|
||||
];
|
||||
|
||||
$tabid = $this->getClientArg('tabid');
|
||||
|
||||
if (!$client_id = $this->getClientArg('client_id')) {
|
||||
$client_id = '';
|
||||
}
|
||||
@@ -148,16 +161,18 @@ class Socket
|
||||
} else {
|
||||
$this->sendToClient($tabid, $client_id, $trace, '');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送给指定客户端
|
||||
* @access protected
|
||||
* @author Zjmainstay
|
||||
* @param $tabid
|
||||
* @param $client_id
|
||||
* @param $logs
|
||||
* @param $force_client_id
|
||||
* @param $tabid
|
||||
* @param $client_id
|
||||
* @param $logs
|
||||
* @param $force_client_id
|
||||
*/
|
||||
protected function sendToClient($tabid, $client_id, $logs, $force_client_id)
|
||||
{
|
||||
@@ -167,20 +182,25 @@ class Socket
|
||||
'logs' => $logs,
|
||||
'force_client_id' => $force_client_id,
|
||||
];
|
||||
|
||||
$msg = @json_encode($logs);
|
||||
$address = '/' . $client_id; //将client_id作为地址, server端通过地址判断将日志发布给谁
|
||||
|
||||
$this->send($this->config['host'], $msg, $address);
|
||||
}
|
||||
|
||||
protected function check()
|
||||
{
|
||||
$tabid = $this->getClientArg('tabid');
|
||||
|
||||
//是否记录日志的检查
|
||||
if (!$tabid && !$this->config['force_client_ids']) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//用户认证
|
||||
$allow_client_ids = $this->config['allow_client_ids'];
|
||||
|
||||
if (!empty($allow_client_ids)) {
|
||||
//通过数组交集得出授权强制推送的client_id
|
||||
$this->allowForceClientIds = array_intersect($allow_client_ids, $this->config['force_client_ids']);
|
||||
@@ -195,6 +215,7 @@ class Socket
|
||||
} else {
|
||||
$this->allowForceClientIds = $this->config['force_client_ids'];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -211,6 +232,7 @@ class Socket
|
||||
if (!isset($_SERVER[$key])) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($args)) {
|
||||
if (!preg_match('/SocketLog\((.*?)\)/', $_SERVER[$key], $match)) {
|
||||
$args = ['tabid' => null];
|
||||
@@ -218,32 +240,39 @@ class Socket
|
||||
}
|
||||
parse_str($match[1], $args);
|
||||
}
|
||||
|
||||
if (isset($args[$name])) {
|
||||
return $args[$name];
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $host - $host of socket server
|
||||
* @param string $message - 发送的消息
|
||||
* @param string $address - 地址
|
||||
* @access protected
|
||||
* @param string $host - $host of socket server
|
||||
* @param string $message - 发送的消息
|
||||
* @param string $address - 地址
|
||||
* @return bool
|
||||
*/
|
||||
protected function send($host, $message = '', $address = '/')
|
||||
{
|
||||
$url = 'http://' . $host . ':' . $this->port . $address;
|
||||
$ch = curl_init();
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
|
||||
$headers = [
|
||||
"Content-Type: application/json;charset=UTF-8",
|
||||
];
|
||||
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置header
|
||||
|
||||
return curl_exec($ch);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user