PHP scrolling Log code implementation _php Tips

Source: Internet
Author: User

PHP Scrolling Log Class Library

PHP log, I have been contacted in accordance with the month of the folder, and then according to the daily document logging way, this way pros and cons, have his use of the scene, I want to say today is another way of logging-file scrolling way log, of course, This scrolling mechanism can also be added to the previous logging method.

How to roll up logs

Scrolling log, as the name suggests, record a module log with a series of log files, the same module file number of restrictions, up to maxnum, the size of the limit, the largest maxsize byte, filename has a certain naming methods, such as: Testlog.log, Testlog_1.log , Testlog_2.log 、、、、、、 where Testlog.log is a log file that is in use, scrolling the log file backward when Testlog.log file size reaches the limit maxsize, as follows:

Copy 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 to limit Maxnum will start the elimination mechanism, delete the oldest log, such as Maxnum set to 10, this time the testlog.log total of up to 10 files, when scrolling if there is testlog_9. Log will start rolling from the Testlog_8.log, cover off the Testlog_9.log, so you can ensure that the log normal records, and will not appear very large log files, to ensure the normal operation of the log system.

code Implementation

<?php Final class LOGS {private $level;
 Private $maxFileNum;
 Private $maxFileSize;
 Private $logPath;

 Private $file;
 Level of the log 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:: $instan
 CE = new self ();
 Return self:: $instance; /** * @Desc initialization * @Param $level int Record level * @Param $maxNum int Maximum log file number * @Param $maxSize int maximum log file size * @Param $l Ogpath string log File save path * @Param $file string log file name prefix * @Return boolean/Public function init ($level, $maxNum, $maxS
 Ize, $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 | |!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).
 "]"; /** * @Desc scrolling record log file/Public function log ($str) {$path = $this->logpath.directory_separator. $this->fi Le. ".
 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_separa TOR. $this->file. " _ ". $index.".
   Log ")) {break;
  The Maxfilenum log file already exists if ($index = = $this->maxfilenum) {$index-; //scrolling 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 Debug information * @Param string log information * @Param string log file * @Param string log Line/Public function debug ($m SG, $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 log file * @Param string log Line/Public function msg ($ms G, $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 string log file * @Param string log Line/Public function err ($ MSG, $file, $line) {if ($this->level<= self::logs_err) {$this->log ($this->formattime (). " [{$file}:{$line}]
 ERR: ${msg}\n ");

 }
 }
}

See an example

#例子中设置记录级别为msg (the debug information is not recorded at this time), the number of log files is 5, the size is 200 bytes (easy to test), file name is called Testlog

$logs = Logs::getinstance ();
$logs->init (1, 5, "./", ' Testlog ');

$logs->msg ("YRT", __file__, __line__);
$logs->debug ("YRT", __file__, __line__);

When we keep running this example, we generate 5 files in the folder where the code is located, as follows:

Testlog_4.log
testlog_3.log
testlog_2.log
testlog_1.log
testlog.log  #最新的日志在这个文件中

The above mentioned is the entire content of this article, I hope you can enjoy.

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.