- 相關(guān)推薦
PHP滾動(dòng)日志的代碼實(shí)現(xiàn)
所謂滾動(dòng)日志,顧名思義,就是記錄一個(gè)模塊的日志用一系列的日志文件,同一模塊文件個(gè)數(shù)有限制,最多maxNum個(gè),大小也有限制,最大maxSize字節(jié),文件名有一定的命名方式,就跟隨百分網(wǎng)小編一起去了解下吧,想了解更多相關(guān)信息請(qǐng)持續(xù)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!
PHP滾動(dòng)日志類(lèi)庫(kù)
PHP記錄日志,我之前接觸過(guò)的有按照年月分文件夾,然后按照日分文件的日志記錄方式,這種方式有利有弊,有他的使用場(chǎng)景,我今天要說(shuō)的是另一種日志記錄方式——文件滾動(dòng)方式記錄日志,當(dāng)然了,這種滾動(dòng)機(jī)制也可以加在前面那種日志記錄方式中。
如何讓日志滾動(dòng)起來(lái)
滾動(dòng)日志,顧名思義,記錄一個(gè)模塊的日志用一系列的日志文件,同一模塊文件個(gè)數(shù)有限制,最多maxNum個(gè),大小也有限制,最大maxSize字節(jié),文件名有一定的命名方式,比如:testlog.log、testlog_1.log,testlog_2.log、、、、、、其中testlog.log是正在使用的日志文件,當(dāng)testlog.log文件大小到達(dá)限制maxSize的時(shí)候就會(huì)向后滾動(dòng)日志文件,就像下面這樣:
復(fù)制代碼 代碼如下:
testlog_2.log -> testlog_3.log
testlog_1.log -> testlog_2.log
testlog.log -> testlog_1.log
testlog.log #0kb
當(dāng)日志文件個(gè)數(shù)到達(dá)限制maxNum的時(shí)候就會(huì)啟動(dòng)淘汰機(jī)制,刪除最老的日志,比如說(shuō)maxNum設(shè)置為10,這個(gè)時(shí)候算上testlog.log一共最多有10個(gè)文件,當(dāng)滾動(dòng)的時(shí)候如果存在testlog_9.log就會(huì)從testlog_8.log開(kāi)始滾動(dòng),覆蓋掉testlog_9.log,這樣就可以保證日志正常記錄,而且不會(huì)出現(xiàn)很大很大的日志文件,保證日志系統(tǒng)的正常運(yùn)行。
代碼實(shí)現(xiàn)
?
1
2
3 <?php
final class LOGS {
private $level;
private $maxFileNum;
private $maxFileSize;
private $logPath;
private $file;
//日志的級(jí)別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 記錄級(jí)別
* @Param $maxNum int 最大日志文件數(shù)目
* @Param $maxSize int 最大日志文件大小
* @Param $logPath string 日志文件保存路徑
* @Param $file string 日志文件名稱(chēng)前綴
* @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 獲取格式化時(shí)間串
*/
public function formatTime()
{
$ustime = explode ( " ", microtime () );
return "[" . date('Y-m-d H:i:s', time()) .".". ($ustime[0] * 1000) . "]";
}
/**
* @Desc 滾動(dòng)方式記錄日志文件
*/
public function log($str)
{
$path = $this->logPath.DIRECTORY_SEPARATOR.$this->file.".log";
clearstatcache();
if(file_exists($path))
{
if(filesize($path) >= $this->maxFileSize)
{
$index = 1;
//獲取最大的滾動(dòng)日志數(shù)目
for(;$index < $this->maxFileNum; $index++)
{
if(!file_exists($this->logPath.DIRECTORY_SEPARATOR.$this->file."_".$index.".log"))
{
break;
}
}
//已經(jīng)存在maxFileNum個(gè)日志文件了
if($index == $this->maxFileNum)
{
$index--;
}
//滾動(dòng)日志
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 記錄錯(cuò)誤信息
* @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");
}
}
}
【PHP滾動(dòng)日志的代碼實(shí)現(xiàn)】相關(guān)文章:
php實(shí)現(xiàn)無(wú)限級(jí)分類(lèi)實(shí)現(xiàn)代碼07-03
PHP中如何實(shí)現(xiàn)crontab代碼05-30
php的curl實(shí)現(xiàn)get和post的代碼07-07
php進(jìn)度條實(shí)現(xiàn)代碼09-20
php代碼如何實(shí)現(xiàn)命令行執(zhí)行05-19
PHP圖片文件怎么上傳實(shí)現(xiàn)代碼10-14