LogController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\User;
  4. use Illuminate\Http\Request;
  5. //<<<<<<< HEAD
  6. use App\Models\AgentOperationLog;
  7. use App\Models\Admin;
  8. //=======
  9. //>>>>>>> 9aa477479386789a8615d89285e74706ec72fbab
  10. use Carbon\Carbon;
  11. class LogController extends Controller
  12. {
  13. public function search(Request $request)
  14. {
  15. $keywords = $request->input('keywords');
  16. $date = $request->input('date', Carbon::now()->toDateString());
  17. if (empty($date)) {
  18. $date = Carbon::now()->toDateString();
  19. }
  20. $isOutRawdata = $request->input('isOutRawdata', false);
  21. $services = explode(',', env('APP_HA'));
  22. $local = $request->server('SERVER_ADDR');
  23. // 获取本地日志
  24. $command = 'cat ' . storage_path('logs/laravel-' . $date . '.log');
  25. return $command;
  26. $commandcrm = 'cat '.storage_path('logs/crm/'.date('Ymd', strtotime($date)).'.log');
  27. foreach (explode('|', $keywords) as $keyword) {
  28. if ($keyword === '' || is_null($keyword))
  29. continue;
  30. $commandcrm .= " | grep '" . $keyword . "'";
  31. $command .= " | grep '" . $keyword . "'";
  32. }
  33. $command .= ' | head -1000';
  34. $commandcrm .= ' | head -1000';
  35. //处理符合条件的日志
  36. exec($command, $logs);
  37. exec($commandcrm, $crmlogs);
  38. $logs = array_merge($logs, $crmlogs);
  39. if ($isOutRawdata) {
  40. return $logs;
  41. }
  42. // 获取其它服务器节点数据
  43. foreach (array_diff($services, [$local]) as $service) {
  44. // 处理data数据
  45. $url = 'http://' . $service . '/log/search-local?' . http_build_query($request->all() + ['isOutRawdata' => 1]);
  46. $data = json_decode(\Http::get($url), true);
  47. if (empty($data))
  48. continue;
  49. $logs = array_merge($logs, $data);
  50. }
  51. //新建一个集合,判断搜索的日志信息是否符合时间戳格式
  52. $resultLogs = collect();
  53. foreach ($logs as $log) {
  54. $dateStr = substr($log, 1, 19);
  55. if (empty(strtotime($dateStr)))
  56. continue;
  57. $resultLogs->push($log);
  58. }
  59. //处理log集合,格式化每条日志开头的时间成时间戳,然后进行排序。
  60. $return = $resultLogs->sortBy(function ($log) {
  61. $dateStr = substr($log, 1, 19);
  62. return strtotime($dateStr);
  63. })->toArray();
  64. //把需要输出的log发送到缓存区,然后获取缓存区内容、使用echo输出变量
  65. ob_start();
  66. dump(array_values($return));
  67. $str = ob_get_contents();
  68. ob_end_clean();
  69. return $str;
  70. }
  71. public function getLogInfoList(Request $request) {
  72. $date = $request->input('date', Carbon::now()->toDateString());
  73. if (empty($date)) {
  74. $date = Carbon::now()->toDateString();
  75. }
  76. // 获取本地日志
  77. $path = storage_path('logs/laravel-' . $date . '.log');
  78. if(file_exists($path)){
  79. $log=file_get_contents($path);
  80. }else{
  81. return $this->success([]);
  82. }
  83. $grep = "/\[\d{4}[-]([01][0-9])[-]([0-3][0-9])[\s]([0-2][0-9])[:]([0-5][0-9])[:]([0-5][0-9])\][\s](local.INFO:)[^\[]*\n/";
  84. preg_match_all($grep,$log,$matchs);
  85. if($matchs[0]){
  86. $list=[];
  87. foreach($matchs[0] as $key=>$val){
  88. $list[$key]['log']=$val;
  89. $list[$key]['time']=mb_substr($val,1,19);
  90. $list[$key]['info']=mb_substr($val,34,-3);
  91. }
  92. return $this->success($list);
  93. }else{
  94. return $this->success([]);
  95. }
  96. }
  97. //<<<<<<< HEAD
  98. public function getLogListInfo(Request $request){
  99. $input=$request->all();
  100. $page_size=$input['page_size'];
  101. $page_index=$input['page_index'];
  102. $num = ($page_index - 1) * $page_size;
  103. $search_name=$input['search_name']??'';
  104. $type=$input['type'];
  105. $role=$input['role'];
  106. $start_time=$input['start_time'];
  107. $end_time=$input['end_time'];
  108. $where=[];
  109. if($type){
  110. $where['type']=$type;
  111. }
  112. $log=AgentOperationLog::with(['user:id,nickname,mobile'])->where($where);
  113. if($search_name){
  114. $ids=$this->getUserIds($search_name);
  115. $log->whereIn('user_id',$ids);
  116. }
  117. if($role){
  118. $admin_ids = $this->getAdminIds($role);
  119. $log->whereIn('admin_id',$admin_ids);
  120. }
  121. if($start_time && $end_time){
  122. $log->whereBetween('created_at',[$start_time,$end_time]);
  123. }
  124. $count=$log->count();
  125. if($count<=0){
  126. $this->error('450001','暂无数据');
  127. }
  128. $list=$log->orderBy('id','desc')->skip($num)->take($page_size)->get();
  129. return $this->success_list($list,'成功',$count);
  130. }
  131. //获取管理员IDS
  132. public function getAdminIds($role){
  133. return Admin::where('role_id',$role)->pluck('id');
  134. }
  135. //获取代理Ids
  136. public function getUserIds($search=null){
  137. return User::where(function($query) use ($search){
  138. $query->where('nickname','like','%'.$search.'%')
  139. ->orwhere('realname','like','%'.$search.'%')
  140. ->orwhere('mobile',$search);
  141. })->withTrashed()->pluck('id');
  142. }
  143. }