123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\User;
- use Illuminate\Http\Request;
- //<<<<<<< HEAD
- use App\Models\AgentOperationLog;
- use App\Models\Admin;
- //=======
- //>>>>>>> 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',$search);
- })->withTrashed()->pluck('id');
- }
- }
|