/**
* 保存日志
* zh+ 2021-12-21 15:56
* */
public function saveLog($request){
$requestInfo = [
'ip' => $request->ip(),
'method' => $request->method(),
'host' => $request->host(),
'uri' => $request->url(),
];
// 获取基本信息
$runtime = number_format(microtime(true) - $this->app->getBeginTime(), 10, '.', '');
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
$mem = number_format((memory_get_usage() - $this->app->getBeginMem()) / 1024, 2);
$logInfo = [
"{$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}",
'运行时间'.number_format((float) $runtime, 6) . 's [ 吞吐率:' . $reqs . 'req/s ] 内存消耗:' . $mem . 'kb 文件加载:' . count(get_included_files()),
'[ info ] [ ROUTE ] ' . var_export($this->getRouteInfo(), true),
'[ info ] [ HEADER ] ' . var_export($request->header(), true),
'[ info ] [ PARAM ] ' . var_export($request->param(), true),
'---------------------------------------------------------------',
];
$logInfo = implode(PHP_EOL, $logInfo) . PHP_EOL;
Log::record($logInfo, 'info');
}
/**
* 获取路由信息
* zh+ 2021-12-21 15:56
* @return array
*/
protected function getRouteInfo(): array
{
$request = app(Request::class);
return [
'rule' => $request->rule()->getRule(),
'route' => $request->rule()->getRoute(),
'option' => $request->rule()->getOption(),
'var' => $request->rule()->getVars(),
];
}
thinkphp6 tp6 类似tp5底层日志 代码
最新推荐文章于 2023-07-13 08:31:00 发布