使用服务器端编程语言(专业超文本预处理器的缩写)读取日志文件,当文件比较大的时候,会报内存不足,因此应该部分读取,读取指定的行数的数据
服务器端编程语言(专业超文本预处理器的缩写)代码:
?phpclass Test{ //日志路径const LOG _ PATH=' e : PHpserver Apache LOgs 错误。日志';const NGINX _ LOG _ PATH=' e : PHpserver NGINX LOgs error。日志';//显示的行数const PAGES=50 public static function main(){ header(' content-type : text/html;charset=utf-8 ');if(!空($ _ GET[' action ']){ self : $ _ GET[' action ']);退出;} }公共静态函数showApacheLogs(){ $ Test=new Test();$ result=$ test-readLogs(self :3360 log _ PATH,self :页);$ html=foreach($ result as $ line){ if(strpos($ line,' error : '){ $ line=' font color=' red ' .$line ./font ';} $html .='div class='line ' ' .$line .' div ';} echo $ html}公共静态函数showenginexlogs(){ $ Test=new Test();$ result=$ test-readLogs(self :3360 ginx _ LOG _ PATH,self :页);$ html=foreach($ result as $ line){ if(strpos($ line,' error '){ $ line=' font color=' red ' .$line ./font ';} $html .='div class='line ' ' .$line .' div ';} echo $ html} /** *读取日志*/私有函数readLogs($filePath,$num=20){ $fp=fopen($filePath,' r ');$ pos=-2;$ eof=$ head=false//当总行数小于数字时,判断是否到第一行了$ lines=array();while($num0){ while($eof!=' n '){ if(f SEK($ FP,$pos,SEEK _ END)==0){//f SEK成功返回0,失败返回-1美元eof=fgetc(FP);$ pos-;}else{ //当到达第一行,行首时,设置$pos失败fseek($fp,0,SEEK _ SET);$ head=true//到达文件头部,开关打开打破;} } array _ unshift($ line,fgets($ FP));if($ head){ break;} //这一句,只能放上一句后,因为到文件头后,把第一行读取出来再跳出整个循环$ eof=$ num-;} fc输($ FP);return array _ reverse($ line);} }测试:3360 main();样式类型=' text/CSS ' * { padd : 0;保证金: 0;}.logsBox { margin :5 xpadding : 5px宽度: 600像素;背景技术: # 000;color : # ffffont-size : 13px;向左浮动:}.logsBox .行{ margin : 12px 0;}/style div class=' logsBox Apache ' div class=' line '日志读取./div/div class=' logsBox enginex ' div class=' line '日志读取./div/div脚本类型=' text/JavaScript ' src=' http :http://apps。bdimg。com/libs/jquery/2。1 .1/jquery。量滴js '/script脚本类型=' text/JavaScript ' $(function(){ function show logs(API,show class){ function read logs(){ $).ajax({ url:api,type:'get ',dataType:'text ',success : function(data){ $(show class)).html(数据);} });} ReadLogs();设置间隔(读取日志,5000);} showLogs('?操作=showNginxLogs ',' .nginx’);显示日志('?action=showApacheLogs ',' .Apache ');});/script以上这篇服务器端编程语言(专业超文本预处理器的缩写)读取大文件并显示的简单实例(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。