laravel框架中搜索日志信息

Bertha 。 2022-04-01 02:20 270阅读 0赞
  1. public function search(Request $request)
  2. {
  3. $keywords = $request->input('keywords');
  4. $date = $request->input('date', Carbon::now()->toDateString());
  5. if (empty($date)) {
  6. $date = Carbon::now()->toDateString();
  7. }
  8. $isOutRawdata = $request->input('isOutRawdata', false);
  9. $services = explode(',', env('APP_HA'));
  10. $local = $request->server('SERVER_ADDR');
  11. // 获取本地日志
  12. $command = 'cat ' . storage_path('logs/laravel-' . $date . '.log');
  13. $commandcrm = 'cat '.storage_path('logs/crm/'.date('Ymd', strtotime($date)).'.log');
  14. foreach (explode('|', $keywords) as $keyword) {
  15. if ($keyword === '' || is_null($keyword))
  16. continue;
  17. $commandcrm .= " | grep '" . $keyword . "'";
  18. $command .= " | grep '" . $keyword . "'";
  19. }
  20. $command .= ' | head -1000';
  21. $commandcrm .= ' | head -1000';
  22. //处理符合条件的日志
  23. exec($command, $logs);
  24. exec($commandcrm, $crmlogs);
  25. $logs = array_merge($logs, $crmlogs);
  26. if ($isOutRawdata) {
  27. return $logs;
  28. }
  29. // 获取其它服务器节点数据
  30. foreach (array_diff($services, [$local]) as $service) {
  31. // 处理data数据
  32. $url = 'http://' . $service . '/log/search-local?' . http_build_query($request->all() + ['isOutRawdata' => 1]);
  33. $data = json_decode(\Http::get($url), true);
  34. if (empty($data))
  35. continue;
  36. $logs = array_merge($logs, $data);
  37. }
  38. //新建一个集合,判断搜索的日志信息是否符合时间戳格式
  39. $resultLogs = collect();
  40. foreach ($logs as $log) {
  41. $dateStr = substr($log, 1, 19);
  42. if (empty(strtotime($dateStr)))
  43. continue;
  44. $resultLogs->push($log);
  45. }
  46. //处理log集合,格式化每条日志开头的时间成时间戳,然后进行排序。
  47. $return = $resultLogs->sortBy(function ($log) {
  48. $dateStr = substr($log, 1, 19);
  49. return strtotime($dateStr);
  50. })->toArray();
  51. //把需要输出的log发送到缓存区,然后获取缓存区内容、使用echo输出变量
  52. ob_start();
  53. dump(array_values($return));
  54. $str = ob_get_contents();
  55. ob_end_clean();
  56. return $str;
  57. }
  58. public function searchLocal(Request $request) {
  59. if (strpos($request->server('HTTP_HOST'), '10.') === 0) {
  60. return $this->search($request);
  61. }
  62. }

发表评论

表情:
评论列表 (有 0 条评论,270人围观)

还没有评论,来说两句吧...

相关阅读