>>>>>> 9aa477479386789a8615d89285e74706ec72fbab use Carbon\Carbon; class LogController extends Controller { 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'); return $command; $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 getLogInfoList(Request $request) { $date = $request->input('date', Carbon::now()->toDateString()); if (empty($date)) { $date = Carbon::now()->toDateString(); } // 获取本地日志 $path = storage_path('logs/laravel-' . $date . '.log'); if(file_exists($path)){ $log=file_get_contents($path); }else{ return $this->success([]); } $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/"; preg_match_all($grep,$log,$matchs); if($matchs[0]){ $list=[]; foreach($matchs[0] as $key=>$val){ $list[$key]['log']=$val; $list[$key]['time']=mb_substr($val,1,19); $list[$key]['info']=mb_substr($val,34,-3); } return $this->success($list); }else{ return $this->success([]); } } //<<<<<<< HEAD public function getLogListInfo(Request $request){ $input=$request->all(); $page_size=$input['page_size']; $page_index=$input['page_index']; $num = ($page_index - 1) * $page_size; $search_name=$input['search_name']??''; $type=$input['type']; $role=$input['role']; $start_time=$input['start_time']; $end_time=$input['end_time']; $where=[]; if($type){ $where['type']=$type; } $log=AgentOperationLog::with(['user:id,nickname,mobile'])->where($where); if($search_name){ $ids=$this->getUserIds($search_name); $log->whereIn('user_id',$ids); } if($role){ $admin_ids = $this->getAdminIds($role); $log->whereIn('admin_id',$admin_ids); } if($start_time && $end_time){ $log->whereBetween('created_at',[$start_time,$end_time]); } $count=$log->count(); if($count<=0){ $this->error('450001','暂无数据'); } $list=$log->orderBy('id','desc')->skip($num)->take($page_size)->get(); return $this->success_list($list,'成功',$count); } //获取管理员IDS public function getAdminIds($role){ return Admin::where('role_id',$role)->pluck('id'); } //获取代理Ids public function getUserIds($search=null){ return User::where(function($query) use ($search){ $query->where('nickname','like','%'.$search.'%') ->orwhere('realname','like','%'.$search.'%') ->orwhere('mobile','like','%'.$search.'%'); })->withTrashed()->pluck('id'); } }