使用PHP读取日志文件,当文件比较大的时候,会报内存不足,因此应该部分读取,读取指定的行数的数据
PHP代码:
<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Test{ </span><span style="color: #008000;">//</span><span style="color: #008000;">日志路径</span> <span style="color: #0000ff;">const</span> LOG_PATH="E:\phpServer\Apache\logs\error.log"<span style="color: #000000;">; </span><span style="color: #0000ff;">const</span> NGINX_LOG_PATH="E:\phpServer\\nginx\logs\error.log"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">显示的行数</span> <span style="color: #0000ff;">const</span> PAGES=50<span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> main(){ </span><span style="color: #008080;">header</span>("content-type:text/html;charset=utf-8"<span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_GET</span>['action'<span style="color: #000000;">])){ self</span>::<span style="color: #800080;">$_GET</span>['action'<span style="color: #000000;">](); </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">; } } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> showApacheLogs(){ </span><span style="color: #800080;">$test</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Test(); </span><span style="color: #800080;">$result</span>=<span style="color: #800080;">$test</span>->readLogs(self::LOG_PATH,self::<span style="color: #000000;">PAGES); </span><span style="color: #800080;">$html</span>=""<span style="color: #000000;">; </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$result</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$line</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">strpos</span>(<span style="color: #800080;">$line</span>,"error:"<span style="color: #000000;">)){ </span><span style="color: #800080;">$line</span>="<font color="red">".<span style="color: #800080;">$line</span>."</font>"<span style="color: #000000;">; } </span><span style="color: #800080;">$html</span>.="<div class="line">".<span style="color: #800080;">$line</span>."<div>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$html</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> showNginxLogs(){ </span><span style="color: #800080;">$test</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Test(); </span><span style="color: #800080;">$result</span>=<span style="color: #800080;">$test</span>->readLogs(self::NGINX_LOG_PATH,self::<span style="color: #000000;">PAGES); </span><span style="color: #800080;">$html</span>=""<span style="color: #000000;">; </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$result</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$line</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">strpos</span>(<span style="color: #800080;">$line</span>,"error"<span style="color: #000000;">)){ </span><span style="color: #800080;">$line</span>="<font color="red">".<span style="color: #800080;">$line</span>."</font>"<span style="color: #000000;">; } </span><span style="color: #800080;">$html</span>.="<div class="line">".<span style="color: #800080;">$line</span>."<div>"<span style="color: #000000;">; } </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$html</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 读取日志 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> readLogs(<span style="color: #800080;">$filePath</span>,<span style="color: #800080;">$num</span>=20<span style="color: #000000;">){ </span><span style="color: #800080;">$fp</span> = <span style="color: #008080;">fopen</span>(<span style="color: #800080;">$filePath</span>,"r"<span style="color: #000000;">); </span><span style="color: #800080;">$pos</span> = -2<span style="color: #000000;">; </span><span style="color: #800080;">$eof</span> = ""<span style="color: #000000;">; </span><span style="color: #800080;">$head</span> = <span style="color: #0000ff;">false</span>; <span style="color: #008000;">//</span><span style="color: #008000;">当总行数小于Num时,判断是否到第一行了 </span> <span style="color: #800080;">$lines</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$num</span>>0<span style="color: #000000;">){ </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$eof</span> != "\n"<span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">fseek</span>(<span style="color: #800080;">$fp</span>, <span style="color: #800080;">$pos</span>, SEEK_END)==0){ <span style="color: #008000;">//</span><span style="color: #008000;">fseek成功返回0,失败返回-1 </span> <span style="color: #800080;">$eof</span> = <span style="color: #008080;">fgetc</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">); </span><span style="color: #800080;">$pos</span>--<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span>{ <span style="color: #008000;">//</span><span style="color: #008000;">当到达第一行,行首时,设置$pos失败 </span> <span style="color: #008080;">fseek</span>(<span style="color: #800080;">$fp</span>,0,<span style="color: #000000;">SEEK_SET); </span><span style="color: #800080;">$head</span> = <span style="color: #0000ff;">true</span>; <span style="color: #008000;">//</span><span style="color: #008000;">到达文件头部,开关打开 </span> <span style="color: #0000ff;">break</span><span style="color: #000000;">; } } </span><span style="color: #008080;">array_unshift</span>(<span style="color: #800080;">$lines</span>,<span style="color: #008080;">fgets</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">)); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$head</span>){ <span style="color: #0000ff;">break</span>; } <span style="color: #008000;">//</span><span style="color: #008000;">这一句,只能放上一句后,因为到文件头后,把第一行读取出来再跳出整个循环 </span> <span style="color: #800080;">$eof</span> = ""<span style="color: #000000;">; </span><span style="color: #800080;">$num</span>--<span style="color: #000000;">; } </span><span style="color: #008080;">fclose</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">array_reverse</span>(<span style="color: #800080;">$lines</span><span style="color: #000000;">); } } Test</span>::<span style="color: #000000;">main(); </span>?> <style type="text/css"> *<span style="color: #000000;">{ padding: 0<span style="color: #000000;">; margin: 0<span style="color: #000000;">; } .<span style="color: #000000;">logsBox{ margin:<span style="color: #000000;">5px; padding:<span style="color: #000000;"> 5px; width:<span style="color: #000000;"> 600px; background: <span style="color: #008000;">#<span style="color: #008000;">000; color:<span style="color: #008000;">#<span style="color: #008000;">fff; font-size:<span style="color: #000000;"> 13px; <span style="color: #0000ff;">float:<span style="color: #000000;"> left; } .logsBox .<span style="color: #000000;">line{ margin: 12px 0<span style="color: #000000;">; } </style> <div style="color: #0000ff;">class="logsBox apache"> <div style="color: #0000ff;">class="line">日志读取...</div> </div> <div style="color: #0000ff;">class="logsBox nginx"> <div style="color: #0000ff;">class="line">日志读取...</div> </div> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"><span style="color: #000000;"> $(<span style="color: #0000ff;">function<span style="color: #000000;">(){ <span style="color: #0000ff;">function showLogs(api,<span style="color: #000000;">showClass){ <span style="color: #0000ff;">function<span style="color: #000000;"> readLogs(){ $.<span style="color: #000000;">ajax({ url:api,<span style="color: #000000;"> type:"get",<span style="color: #000000;"> dataType:"text",<span style="color: #000000;"> success:<span style="color: #0000ff;">function<span style="color: #000000;">(data){ $(showClass).<span style="color: #000000;">html(data); } }); } readLogs(); setInterval(readLogs,5000<span style="color: #000000;">); } showLogs("?action=showNginxLogs",".nginx"<span style="color: #000000;">); showLogs("?action=showApacheLogs",".apache"<span style="color: #000000;">); }); </script> </div> <p> </p> </div> <div id="MySignature"></div> <div class="clear"></div> </div> </div>