Home >php教程 >php手册 >PHP logging to debug code

PHP logging to debug code

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-08-08 08:50:031808browse

Thinkphp comes with its own log system, and the generated files are under Runtime. Today we will introduce a natively written log insertion method to record the visitor's IP, access time and browser type.
PHP logging to debug code
Generate log class <?php <br /> /*<br> * Log records<br> * Generate a log file every day. When the file exceeds the specified size, back up the log file and regenerate a new log file<br> *<br> */<br> class Log {<br> <br> Private $maxsize = 1024000; //Maximum file size 1M<br> ​ <br> //Write to log<br> Public function writeLog($filename,$msg){<br>          $res = array();<br>          $res['msg'] = $msg;<br>          $res['logtime'] = date("Y-m-d H:i:s",time());<br> <br> ​​​​//If the log file exceeds the specified size, back up the log file<br> If(file_exists($filename) && (abs(filesize($filename)) > $this->maxsize)){<br>                  $newfilename = dirname($filename).'/'.time().'-'.basename($filename);<br>                                                                                                                                                                                                                             rename($filename, $newfilename);<br>         }<br> <br> ​​​​//If it is a new log file, remove the first character comma in the content<br> If(file_exists($filename) && abs(filesize($filename))>0){<br>              $content = ",".json_encode($res);<br>           }else{<br>               $content = json_encode($res);<br>         }<br> <br>                 //Append the log content after the log file content<br>           file_put_contents($filename, $content, FILE_APPEND);<br> }<br> <br> <br> //Read log<br> Public function readLog($filename){<br> If(file_exists($filename)){<br>               $content = file_get_contents($filename);<br>                $json = json_decode('['.$content.']',true);<br>           }else{<br>              $json = '{"msg":"The file does not exist."}';<br>         }<br> Return $ json; <br> }<br> }<br> ?>Write and read logs$Log = new Log();<br> $Log->writeLog($filename, $msg);<br> $loglist = $Log->readLog($filename);View the generated log: http://www.sucaihuo.com/js/903.html

PHP logging to debug code PHP records user access IP, browser type and access time to txt log file.zip ( 5.08 KB Download: 0 times )

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn