PHP scrolling Log Code implementation, PHP scrolling log
PHP Scrolling Log Class Library
PHP logging, I have been contacted before the date of the folder, and then according to the log records of the day, this way, there are pros and cons, there is his use of the scene, I would like to say today is another way of logging-file scrolling way to log, of course, This kind of scrolling mechanism can also be added to the previous type of logging method.
How to make the log roll up
Scroll log, as the name implies, record a module of the log with a series of log files, the number of the same module is limited, up to Maxnum, the size of the limit, the maximum maxsize bytes, filenames have certain naming methods, such as: Testlog.log, Testlog_1.log , Testlog_2.log 、、、、、、 where Testlog.log is the log file being used, and when the Testlog.log file size reaches the limit maxsize the log file is scrolled back, as follows:
Copy the Code code 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 reached the limit maxnum will start the elimination mechanism, delete the oldest logs, such as Maxnum set to 10, this time count Testlog.log altogether up to 10 files, when scrolling if there is testlog_9. Log will start rolling from the Testlog_8.log, overwriting the testlog_9.log, so that the log can be guaranteed to record, and there will not be very 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;//day The level of 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 = n EW 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 Save path * @Param $file string log file name prefix * @Return Boolean */Public function init ($level, $maxNum, $maxSize, $logPa Th, $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 | |!i S_dir ($logPath)) {return false;} $this->level = $level; $this->maxfilenum = $maXnum; $this->maxfilesize = $maxSize; $this->logpath = $logPath; $this->file = $file; return true; }/** * @Desc get the formatted time string */Public Function formattime () {$ustime = Explode ("", Microtime ()); Return "[". Date (' y-m-d h:i:s ', Time ()). ".". ($ustime [0] * 1000). "]"; /** * Log File @Desc scrolling method */Public function log ($str) {$path = $this->logpath.directory_separator. $this->file. Log "; Clearstatcache (); if (file_exists ($path)) {if (FileSize ($path) >= $this->maxfilesize) {$index = 1; Gets the maximum number of scroll logs for (; $index < $this->maxfilenum; $index + +) {if (!file_exists ($this->logpath.directory_ SEPARATOR. $this->file. " _ ". $index.". Log ")) {break; }}//Maxfilenum log file already exists if ($index = = $this->maxfilenum) {$index--; }//scroll logs 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 Log Debug information * @Param string log information * @Param the file where the string log is located @Param the row of string log */Public function debug ($msg, $fil E, $line) {if ($this->level <= self::logs_debug) {$this->log ($this->formattime (). " [{$file}:{$line}] DEBUG: ${msg}\n "); }}/** * @Desc log information * @Param string log information * @Param the file where the string log is located @Param the row of string Journal */Public Function msg ($msg, $file, $line) {if ($this->level <= self::logs_msg) {$this->log ($this->formattime (). " [{$file}:{$line}] MSG: ${msg}\n "); }}/** * @Desc log error message * @Param string log information * @Param the file where the string log is located @Param the row of string log */Public Function err ($msg, $fil E, $line) {if ($this->level <= self::logs_err) {$this->log ($this->formattime (). " [{$file}:{$line}] ERR: ${msg}\n "); } }}
Look at an example.
#例子中设置记录级别为msg (the debug information is not logged at this time), the number of log files is 5, the size is 200 bytes (easy to test), the filename is called Testlog
$logs = Logs::getinstance (); $logs->init (1, 5, $, "./", ' Testlog '); $logs->msg ("YRT", __file__, __line__); $logs- >debug ("YRT", __file__, __line__);
When we run this example all the time, we generate 5 files in the folder where the code is located, as follows:
Testlog_4.logtestlog_3.logtestlog_2.logtestlog_1.logtestlog.log #最新的日志在这个文件中
The above mentioned is the whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/1014435.html www.bkjia.com true http://www.bkjia.com/PHPjc/1014435.html techarticle PHP Rolling Log Code implementation, PHP scrolling log php rolling log class library PHP log, I have been exposed to the folder according to the date, and then according to the daily sub-file of the log record side ...