PHP rolling log code implementation, php Rolling Log _ PHP Tutorial

Source: Internet
Author: User
PHP rolling log code implementation, php rolling log. PHP rolling log code implementation, php rolling log PHP rolling log class library PHP record log, what I have previously contacted is by year and month folder, then, the PHP rolling log code is implemented based on the log record of the daily file, and the php rolling log

PHP rolling log class library

PHP records logs. I have previously used folders by year and month, and recorded logs by day and file. this method has advantages and disadvantages and has application scenarios, what I want to talk about today is another log record method-file rolling method to record logs. of course, this rolling mechanism can also be added to the previous logging method.

How to scroll up logs

Rolling logs, as the name implies, use a series of log files to record the logs of a module. the number of files in a module is limited. The maximum number of files is maxNum, and the size is also limited. The maximum value is maxSize, the file name can be named in a certain way, such as testlog. log, testlog_1.log, testlog_2.log, and testlog. log is a log file in use, when testlog. when the log file size reaches the limit of maxSize, it will scroll back to the log file, as shown below:

The code is as follows:
Testlog_2.log-> testlog_3.log
Testlog_1.log-> testlog_2.log
Testlog. log-> testlog_1.log
Testlog. log # 0kb

When the number of log files reaches the limit of maxNum, the elimination mechanism is enabled to delete the oldest logs. for example, if maxNum is set to 10, testlog is counted. log has a total of 10 files. if testlog_9.log exists during scrolling, it will start to scroll from testlog_8.log and overwrite testlog_9.log. This ensures normal log records, there will be no large log files to ensure the normal operation of the log system.

Code implementation

<? Phpfinal class LOGS {private $ level; private $ maxFileNum; private $ maxFileSize; private $ logPath; private $ file; // log level DEBUG, MSG, ERR const LOGS_DEBUG = 0; const LOGS_MSG = 1; const LOGS_ERR = 2; private static $ instance = null; private function _ construct () {} public static function getInstance () {if (self :: $ instance = null) {self: $ instance = new self ();} return self: $ instance;}/*** @ Desc Initialize * @ Param $ level int record level * @ Param $ maxNum int maximum number of log files * @ Param $ maxSize int maximum log file size * @ Param $ logPath string log file storage path * @ Param $ file string log file name prefix * @ Return boolean */public function init ($ level, $ maxNum, $ maxSize, $ logPath, $ file) {$ level = intval ($ level); $ maxNum = intval ($ maxNum ); $ maxSize = intval ($ maxSize );! Is_dir ($ logPath) & mkdir ($ logPath, 0777, true); if (! In_array ($ level, array (self: LOGS_DEBUG, self: LOGS_MSG, self: LOGS_ERR) | $ maxNum <= 0 | $ maxSize <= 0 |! Is_dir ($ logPath) {return false;} $ this-> level = $ level; $ this-> maxFileNum = $ maxNum; $ this-> maxFileSize = $ maxSize; $ this-> logPath = $ logPath; $ this-> file = $ file; return true;}/*** @ Desc get the formatting Time String */public function formatTime () {$ ustime = explode ("", microtime (); return "[". date ('Y-m-d H: I: S', time ()). ". ". ($ ustime [0] * 1000 ). "]";}/*** @ Desc scroll mode to record the log file */public function log ($ Str) {$ path = $ this-> logPath. DIRECTORY_SEPARATOR. $ this-> file. ". log "; clearstatcache (); if (file_exists ($ path) {if (filesize ($ path) >=$ this-> maxFileSize) {$ index = 1; // obtain the maximum number of rolling logs for (; $ index <$ this-> maxFileNum; $ index ++) {if (! File_exists ($ this-> logPath. DIRECTORY_SEPARATOR. $ this-> file. "_". $ index. ". log ") {break ;}/// a maxFileNum log file already exists. if ($ index =$ this-> maxFileNum) {$ index --;} // rolling log for (; $ index> 1; $ index --) {$ new = $ this-> logPath. DIRECTORY_SEPARATOR. $ this-> file. "_". $ index. ". log "; $ old = $ this-> logPath. DIRECTORY_SEPARATOR. $ this-> file. "_". ($ index-1 ). ". log "; rename ($ old, $ new) ;}$ newFile = $ this-> logPath. DIRECTORY_SEPARATOR. $ this-> file. "_ 1.log"; rename ($ path, $ newFile) ;}}$ fp = fopen ($ path," a + B "); fwrite ($ fp, $ str, strlen ($ str); fclose ($ fp); return true ;} /*** @ Desc record debugging information * @ Param string log file * @ Param string log line */public function debug ($ msg, $ file, $ line) {if ($ this-> level <= self: LOGS_DEBUG) {$ this-> log ($ this-> formatTime (). "[{$ file }:{ $ line}] DEBUG :$ {msg} \ n ");}} /*** @ Desc record information * @ Param string log information * @ Param string the log file * @ Param string the log line */public function msg ($ msg, $ file, $ line) {if ($ this-> level <= self: LOGS_MSG) {$ this-> log ($ this-> formatTime (). "[{$ file }:{ $ line}] MSG :$ {msg} \ n ");}} /*** @ Desc record error information * @ Param string log information * @ Param string the log file * @ Param string the log line */public function err ($ msg, $ file, $ line) {if ($ this-> level <= self: LOGS_ERR) {$ this-> log ($ this-> formatTime (). "[{$ file }:{ $ line}] ERR :$ {msg} \ n ");}}}

Let's look at an example.

# In the example, set the record level to msg (the debug information will not be recorded at this time), the number of log files is 5, and the size is 200 bytes (for Test convenience). the file name is testlog.

$logs = LOGS::getInstance();$logs->init(1, 5, 200, "./", 'testlog');$logs->msg("YRT", __FILE__, __LINE__);$logs->debug("YRT", __FILE__, __LINE__);

When we keep running this example, five files will be generated in the folder where the code is located, as shown below:

Testlog_4.logtestlog_3.logtestlog_2.logtestlog_1.logtestlog.log # The latest log is in this file.

The above is all the content of this article. I hope you will like it.

The rolling PHP log Library PHP records the logs. I have previously encountered folders by year and month, and then the log record by day and file...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.