public function search(Request $request)
{
$keywords = $request->input('keywords');
$date = $request->input('date', Carbon::now()->toDateString());
if (empty($date)) {
$date = Carbon::now()->toDateString();
}
$isOutRawdata = $request->input('isOutRawdata', false);
$services = explode(',', env('APP_HA'));
$local = $request->server('SERVER_ADDR');
// 获取本地日志
$command = 'cat ' . storage_path('logs/laravel-' . $date . '.log');
$commandcrm = 'cat '.storage_path('logs/crm/'.date('Ymd', strtotime($date)).'.log');
foreach (explode('|', $keywords) as $keyword) {
if ($keyword === '' || is_null($keyword))
continue;
$commandcrm .= " | grep '" . $keyword . "'";
$command .= " | grep '" . $keyword . "'";
}
$command .= ' | head -1000';
$commandcrm .= ' | head -1000';
//处理符合条件的日志
exec($command, $logs);
exec($commandcrm, $crmlogs);
$logs = array_merge($logs, $crmlogs);
if ($isOutRawdata) {
return $logs;
}
// 获取其它服务器节点数据
foreach (array_diff($services, [$local]) as $service) {
// 处理data数据
$url = 'http://' . $service . '/log/search-local?' . http_build_query($request->all() + ['isOutRawdata' => 1]);
$data = json_decode(\Http::get($url), true);
if (empty($data))
continue;
$logs = array_merge($logs, $data);
}
//新建一个集合,判断搜索的日志信息是否符合时间戳格式
$resultLogs = collect();
foreach ($logs as $log) {
$dateStr = substr($log, 1, 19);
if (empty(strtotime($dateStr)))
continue;
$resultLogs->push($log);
}
//处理log集合,格式化每条日志开头的时间成时间戳,然后进行排序。
$return = $resultLogs->sortBy(function ($log) {
$dateStr = substr($log, 1, 19);
return strtotime($dateStr);
})->toArray();
//把需要输出的log发送到缓存区,然后获取缓存区内容、使用echo输出变量
ob_start();
dump(array_values($return));
$str = ob_get_contents();
ob_end_clean();
return $str;
}
public function searchLocal(Request $request) {
if (strpos($request->server('HTTP_HOST'), '10.') === 0) {
return $this->search($request);
}
}
还没有评论,来说两句吧...