读取日志文件开发总结

最后实现:

读取日志文件开发总结_第1张图片

查询条件

  1. 关键字搜索  2.关键字+开始日期   3.开始日期+结束日期   4.关键字+时间段

技术点:

  1. 读取文件

    1. 本人没涉及过PHP文件读取,都是现用现看 菜了

    2. 正则匹配截取出相应的字符串(浪费了很长时间,因为自己正则也是个白蛋)

    3. 把截取出的字符串压入数组并返回作为最后操作的数据库

    function readLogFile($date,$param=array()){
        $filename="../../client/http/MonitorLog/$date.log";
        if (file_exists($filename)) {
            $handle=@fopen($filename, "r");  //获取文件资源句柄
            $log_list=array();
            $i=0;
            if ($handle) {
                while(!feof($handle)){
                    $buffer=fgets($handle,4096);  //读取每行的字符串
                    $i++;
                    $time=substr($buffer,0,19);
                    preg_match_all("/(?:\()(.*)(?:\))/i",$buffer,$matches);
                    $phone=$matches[1][0];
    
                    preg_match_all("/(?:\s)(.*)(?:\()/i",substr($buffer,19),$matches);
                    $user_name=$matches[1][0];
    
                    preg_match_all("/(?:\辆)((\d)*)/",substr($buffer,19),$matches);
                    $car_id=$matches[1][0];
    
                    if(!empty($car_id)){
                        $carInfo=getCarInfo($car_id);
                        $number=$carInfo['number'];
                        $model=$carInfo['model'];
                        $brand=$carInfo['brand'];
    
                    }
    
                    preg_match_all("/(?:\发)(.*)/",substr($buffer,19),$matches);
                    $content=$matches[0][0];
    
                    if( is_array($param) && count($param) !=0 ){  //查询时 根据条件获取数据
                        if($param['keyword']==$phone || $param['keyword']==$number){
                            $log_list[$i]['phone']=$phone;
                            $log_list[$i]['number']=$number;
                            $log_list[$i]['model']=$model;
                            $log_list[$i]['brand']=$brand;
                            $log_list[$i]['car_id']=$car_id;
                            $log_list[$i]['user_name']=$user_name;
                            $log_list[$i]['time']=$time;
                            $log_list[$i]['content']=$content;
                        }
                    }else{
                        $log_list[$i]['phone']=$phone;
                        $log_list[$i]['number']=$number;
                        $log_list[$i]['model']=$model;
                        $log_list[$i]['brand']=$brand;
                        $log_list[$i]['car_id']=$car_id;
                        $log_list[$i]['user_name']=$user_name;
                        $log_list[$i]['time']=$time;
                        $log_list[$i]['content']=$content;
                    }
                }
                fclose($handle);  //关闭资源句柄
            }
            array_pop($log_list);
            array_pop($log_list);
            return $log_list;  //返回最后的数据
    
        }
    }
  2. 2.分页操作

    a.分页利用Ajax分页,

            点击下一页的时候利用ajax 把条件(比如说查询条件)以及当前第几页page参数传过来,提交给query函数,根据条件查询出相应的数据作为最后的数据库,

    b.利用PHP 核心函数  array_slice实现对数组的分页显示

    /**
     * 数组分页函数  核心函数  array_slice
     * 用此函数之前要先将数据库里面的所有数据按一定的顺序查询出来存入数组中
     * $count   每页多少条数据
     * $page   当前第几页
     * $array   查询出来的所有数组
     * order 0 - 不变     1- 反序
     */
    function page_array($count,$page,$array,$order,$keyword,$date_start,$date_after){
    
        global $countPage; #定全局变量
        $page=(empty($page))?'1':$page; #判断当前页面是否为空 如果为空就表示为第一页面
        $start=($page-1)*$count; #计算每次分页的开始位置
        if($order==1){
            $array=array_reverse($array);
        }
        $totals=count($array);
        $countPage=ceil($totals/$count); #计算总页面数
        $pageData=array();
        $pageData=array_slice($array,$start,$count);
        #返回查询参数,以便再次点击下一页时会根据查询条件查询相应的数据
        $filter['page']=$page;
        $filter['page_size']=$count;
        $filter['keyword']=$keyword;
        $filter['date_start']=$date_start;
        $filter['date_end']=$date_after;
    
        return array('result' => $pageData,  'page_count' => $countPage,'filter' =>$filter,'record_count'=>$totals);
    }

  3. 问题:

    1.日志文件不存在时,无返回,直接断死,(比如查询一个时间段,结束日期或开始日期过长没有日志文件,则中间有的也不显示,因为当遇到文件不存在时直接报了异常)

            在读取文件之前先判断日志文件是否存在

    function checkFile($date){
        $filename="../../client/http/MonitorLog/$date.log";
        if(file_exists($filename)){
            return true;
        }
        return false;
    }
  4. elseif(empty($keyword) && !empty($date_start)){
            $log_list=array();
            for($date_start;$date_start<=$date_end;$date_start+=86400){
                if(checkFile(date('Y-m-d',$date_start))){
                    $result =  getLog(date('Y-m-d',$date_start),array());
                    $log_list=array_merge($log_list,$result);
                }
    
    
            }
        }
  5. 2.正则匹配需要多加练习

    3.灵活使用for循环--死角使用数字

            求时间段时自己先把大几天算出来 然后

    for($int=0;$int<=$days;$int++)
    {
        然后加上86400
        再转换成时间格式date('Y-m-d',$date_start)
    }
    何其繁琐,下面的方法精炼简单
  6.  for($date_start;$date_start<=$date_end;$date_start+=86400){
         date('Y-m-d',$date_start)
     }
  7.     时间戳比较,下面用时直接转换成日期格式

    4.代码逻辑模块化

        条理清晰,放便调试

        需要多看别人的代码,多想,多写多练,才能提升

    总结:遇到一次困难就是一次成长的经历,要敢于面对,努力尝试,总结弱点,多加练习,再下一次遇到时轻而易举地顺利拿下


你可能感兴趣的:(文件操作,数组分页,日志读取)