PHP滾動日志的代碼實現(xiàn)
PHP滾動日志類庫
PHP記錄日志,我之前接觸過的有按照年月分文件夾,然后按照日分文件的日志記錄方式,這種方式有利有弊,有他的使用場景,我今天要說的是另一種日志記錄方式——文件滾動方式記錄日志,當然了,這種滾動機制也可以加在前面那種日志記錄方式中。
如何讓日志滾動起來
滾動日志,顧名思義,記錄一個模塊的日志用一系列的日志文件,同一模塊文件個數(shù)有限制,最多maxNum個,大小也有限制,最大maxSize字節(jié),文件名有一定的命名方式,比如:testlog.log、testlog_1.log,testlog_2.log、、、、、、其中testlog.log是正在使用的日志文件,當testlog.log文件大小到達限制maxSize的時候就會向后滾動日志文件,就像下面這樣:
testlog_2.log -> testlog_3.log
testlog_1.log -> testlog_2.log
testlog.log -> testlog_1.log
testlog.log #0kb
當日志文件個數(shù)到達限制maxNum的時候就會啟動淘汰機制,刪除最老的日志,比如說maxNum設(shè)置為10,這個時候算上testlog.log一共最多有10個文件,當滾動的時候如果存在testlog_9.log就會從testlog_8.log開始滾動,覆蓋掉testlog_9.log,這樣就可以保證日志正常記錄,而且不會出現(xiàn)很大很大的日志文件,保證日志系統(tǒng)的正常運行。
代碼實現(xiàn)
<?php final class LOGS { private $level; private $maxFileNum; private $maxFileSize; private $logPath; private $file; //日志的級別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 初始化 * @Param $level int 記錄級別 * @Param $maxNum int 最大日志文件數(shù)目 * @Param $maxSize int 最大日志文件大小 * @Param $logPath string 日志文件保存路徑 * @Param $file string 日志文件名稱前綴 * @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 獲取格式化時間串 */ public function formatTime() { $ustime = explode ( " ", microtime () ); return "[" . date('Y-m-d H:i:s', time()) .".". ($ustime[0] * 1000) . "]"; } /** * @Desc 滾動方式記錄日志文件 */ public function log($str) { $path = $this->logPath.DIRECTORY_SEPARATOR.$this->file.".log"; clearstatcache(); if(file_exists($path)) { if(filesize($path) >= $this->maxFileSize) { $index = 1; //獲取最大的滾動日志數(shù)目 for(;$index < $this->maxFileNum; $index++) { if(!file_exists($this->logPath.DIRECTORY_SEPARATOR.$this->file."_".$index.".log")) { break; } } //已經(jīng)存在maxFileNum個日志文件了 if($index == $this->maxFileNum) { $index--; } //滾動日志 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 記錄調(diào)試信息 * @Param string 日志信息 * @Param string 日志所在文件 * @Param string 日志所在行 */ public function debug($msg, $file, $line) { if($this->level <= self::LOGS_DEBUG) { $this->log($this->formatTime()."[{$file}:{$line}]DEBUG: ${msg}\n"); } } /** * @Desc 記錄信息 * @Param string 日志信息 * @Param string 日志所在文件 * @Param string 日志所在行 */ public function msg($msg, $file, $line) { if($this->level <= self::LOGS_MSG) { $this->log($this->formatTime()."[{$file}:{$line}]MSG: ${msg}\n"); } } /** * @Desc 記錄錯誤信息 * @Param string 日志信息 * @Param string 日志所在文件 * @Param string 日志所在行 */ public function err($msg, $file, $line) { if($this->level <= self::LOGS_ERR) { $this->log($this->formatTime()."[{$file}:{$line}]ERR: ${msg}\n"); } } }
看個例子
#例子中設(shè)置記錄級別為msg(此時debug信息是不會紀錄的),日志文件個數(shù)為5,大小為200個字節(jié)(測試方便),文件名稱為testlog
$logs = LOGS::getInstance(); $logs->init(1, 5, 200, "./", 'testlog'); $logs->msg("YRT", __FILE__, __LINE__); $logs->debug("YRT", __FILE__, __LINE__);
當我們不停的運行這個例子的時候,會在代碼所在文件夾下生成5個文件就像下面這樣:
testlog_4.log testlog_3.log testlog_2.log testlog_1.log testlog.log #最新的日志在這個文件中
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
- PHP 寫文本日志實現(xiàn)代碼
- 無法在發(fā)生錯誤時創(chuàng)建會話,請檢查 PHP 或網(wǎng)站服務(wù)器日志,并正確配置 PHP 安裝最快的解決辦法
- php記錄日志的實現(xiàn)代碼
- 無法在發(fā)生錯誤時創(chuàng)建會話,請檢查 PHP 或網(wǎng)站服務(wù)器日志,并正確配置 PHP 安裝(win+linux)
- 記錄PHP錯誤日志 display_errors與log_errors的區(qū)別
- 實現(xiàn)Nginx中使用PHP-FPM時記錄PHP錯誤日志的配置方法
- PHP.ini中配置屏蔽錯誤信息顯示和保存錯誤日志的例子
- PHP寫日志的實現(xiàn)方法
相關(guān)文章
PHP 處理TXT文件(打開/關(guān)閉/檢查/讀取)
php寫入txt、php讀取txt,在PHP處理TXT文件時經(jīng)常會使用到,本文整理了一些相關(guān)代碼,感興趣的朋友可以參考下哈2013-05-05PHP和Shell實現(xiàn)檢查SAMBA與NFS Server是否存在
這篇文章主要介紹了PHP和Shell實現(xiàn)檢查SAMBA與NFS Server是否存在,本文分別給出了PHP檢查腳本和Shell檢查腳本,需要的朋友可以參考下2015-01-01PHP實現(xiàn)在數(shù)據(jù)庫百萬條數(shù)據(jù)中隨機獲取20條記錄的方法
這篇文章主要介紹了PHP實現(xiàn)在數(shù)據(jù)庫百萬條數(shù)據(jù)中隨機獲取20條記錄的方法,涉及php簡單數(shù)據(jù)庫查詢與隨機數(shù)操作相關(guān)技巧,需要的朋友可以參考下2017-04-04