nqkk58 发表于 2024-10-4 19:57:25

PHP记录和读取JSON格式日志文件


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>有时需要记录用户<span style="color: black;">或</span>后端的某个操作事件的运行<span style="color: black;">状况</span>,<span style="color: black;">能够</span><span style="color: black;">运用</span>后端语言如PHP将操作结果记录到日志文件中,方便测试和<span style="color: black;">查询</span>问题。尤其是这些在后端运行的而前端<span style="color: black;">不可</span>直接看到运行结果的,<span style="color: black;">那样</span>就<span style="color: black;">能够</span>用日志文件记录下来,<span style="color: black;">倘若</span>你经常跟<span style="color: black;">有些</span>接口<span style="color: black;">研发</span>如支付宝接口、<span style="color: black;">微X</span>卡券接口打交道的话,日志记录就必不可少了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>讲的PHP记录日志,<span style="color: black;">便是</span>将日志信息写入到一个日志文件中,区别于内存日志。写入日志的流程是:打开日志文件(<span style="color: black;">倘若</span>不存在则新创建),<span style="color: black;">而后</span>将日志内容追加到日志文件的后面,最后关闭日志文件。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本文中,<span style="color: black;">咱们</span>将日志内容以json个格式<span style="color: black;">保留</span>,方便必要时直接读取。</p>PHP写日志文件<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PHP写日志文件需要打开、写入和关闭文件等操作,PHP有fopen(),fwrite()和fclose()三个函数与之对应,而另一个函数
      file_put_contents()它<span style="color: black;">亦</span>能字符串写入文件,其实这个函数实现了依次调用 fopen(),fwrite() 以及
      fclose()。<span style="color: black;">因此</span><span style="color: black;">咱们</span><span style="color: black;">运用</span>file_put_contents()非常简洁。值得<span style="color: black;">重视</span>的是,往文件后面追加内容时需要带上参
      数:FILE_APPEND。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">实质</span>运行中,<span style="color: black;">咱们</span>有可能会遇到日志文件超大的<span style="color: black;">状况</span>,<span style="color: black;">因此</span><span style="color: black;">咱们</span>设置一个最大值,当日志文件<span style="color: black;">体积</span>超过这个最大值时,将此日志文件备份好,<span style="color: black;">而后</span>重新生成一个新的日志文件来记录新的日志内容。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在写日志前,<span style="color: black;">咱们</span>将日志内容进行json格式化,<span style="color: black;">因此</span>需要将内容转化成JSON格式,<span style="color: black;">而后</span>写入文件。当然你<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">不消</span>json,<span style="color: black;">或</span>换作别的工具程序(如日志分析工具)<span style="color: black;">能够</span>阅读的格式。总之,<span style="color: black;">咱们</span>写入的内容是方便必要时<span style="color: black;">能够</span>方便读取。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">function writeLog($filename,$msg){ </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$res = array();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $res = $msg; </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $res = date("Y-m-d H:i:s",time()); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //<span style="color: black;">倘若</span>日志文件超过了指定<span style="color: black;">体积</span>则备份日志文件 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if(file_exists($filename) &amp;&amp; (abs(filesize($filename)) &gt; 1024000)){</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $newfilename = dirname($filename)./.time().-.basename($filename); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rename($filename, $newfilename); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //<span style="color: black;">倘若</span>是新建的日志文件,去掉内容中的<span style="color: black;">第1</span>个字符逗号 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if(file_exists($filename) &amp;&amp; abs(filesize($filename))&gt;0){</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $content = ",".json_encode($res); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else{ </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $content = json_encode($res); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //往日志文件内容后面追加日志内容 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">file_put_contents($filename, $content, FILE_APPEND);</p>}PHP读日志文件<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">必要时,<span style="color: black;">咱们</span>会读取日志内容进行分析,<span style="color: black;">一样</span><span style="color: black;">咱们</span><span style="color: black;">运用</span>PHP的file_get_contents()函数,直接将内容读取,并且转换成json格式,方便调用。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">function readLog($filename){ </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if(file_exists($filename)){</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $content = file_get_contents($filename); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $json = json_decode([.$content.],true); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else{ </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $json = {"msg":"The file does not exist."}; </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">return $json;</p>}日志写入和读取类<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">写入和读取日志的功能<span style="color: black;">咱们</span>经常要用到,<span style="color: black;">因此</span>我将写入和读取功能整理成类,方便调用。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/* </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;* 日志类 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;* <span style="color: black;">每日</span>生成一个日志文件,当文件超过指定<span style="color: black;">体积</span>则备份日志文件并重新生成新的日志文件 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">*/ </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">class Log { </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">private $maxsize = 1024000; //最大文件<span style="color: black;">体积</span>1M</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp; //写入日志 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp; public function writeLog($filename,$msg){ </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $res = array(); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $res = $msg; </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $res = date("Y-m-d H:i:s",time()); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //<span style="color: black;">倘若</span>日志文件超过了指定<span style="color: black;">体积</span>则备份日志文件 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if(file_exists($filename) &amp;&amp; (abs(filesize($filename)) &gt; $this-&gt;maxsize)){</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $newfilename = dirname($filename)./.time().-.basename($filename); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">rename($filename, $newfilename);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //<span style="color: black;">倘若</span>是新建的日志文件,去掉内容中的<span style="color: black;">第1</span>个字符逗号 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(file_exists($filename) &amp;&amp; abs(filesize($filename))&gt;0){ </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $content = ",".json_encode($res); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else{ </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$content = json_encode($res);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //往日志文件内容后面追加日志内容 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file_put_contents($filename, $content, FILE_APPEND); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp; } </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp; //读取日志 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp; public function readLog($filename){ </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if(file_exists($filename)){</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $content = file_get_contents($filename); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $json = json_decode([.$content.],true); </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else{ </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $json = {"msg":"The file does not exist."}; </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return $json; </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">} </p>?&gt;
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span><span style="color: black;">办法</span>:</p><span style="color: black;">$</span><span style="color: black;">filename</span>&nbsp;=&nbsp;<span style="color: black;">"logs/log_"</span>.date(<span style="color: black;">"Ymd"</span>,time()).<span style="color: black;">".txt"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;&nbsp;</p><span style="color: black;">$</span><span style="color: black;">msg</span>&nbsp;=&nbsp;<span style="color: black;">写入了日志</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;&nbsp;</p><span style="color: black;">$</span><span style="color: black;">Log</span>&nbsp;=&nbsp;<span style="color: black;">new</span>&nbsp;Log();&nbsp;<span style="color: black;">//实例化</span><span style="color: black;">$</span><span style="color: black;">Log</span>-&gt;writeLog(<span style="color: black;">$</span><span style="color: black;">filename</span>,<span style="color: black;">$</span><span style="color: black;">msg</span>);&nbsp;<span style="color: black;">//写入日志</span><span style="color: black;">$</span><span style="color: black;">loglist</span>&nbsp;=&nbsp;<span style="color: black;">$</span><span style="color: black;">Log</span>-&gt;readLog(<span style="color: black;">$</span><span style="color: black;">filename</span>);&nbsp;<span style="color: black;">//读取日志</span>




nqkk58 发表于 2024-10-16 12:19:11

楼主继续加油啊!外链论坛加油!

4zhvml8 发表于 2024-11-12 21:15:51

大势所趋,用于讽刺一些制作目的就是为了跟风玩梗,博取眼球的作品。
页: [1]
查看完整版本: PHP记录和读取JSON格式日志文件