PHP滾動日誌的代碼實現_PHP教程

來源:互聯網
上載者:User

PHP滾動日誌的代碼實現


PHP滾動日誌類庫

PHP記錄日誌,我之前接觸過的有按照年月分檔案夾,然後按照日分檔案的日誌記錄方式,這種方式有利有弊,有他的使用情境,我今天要說的是另一種日誌記錄方式——檔案滾動方式記錄日誌,當然了,這種滾動機制也可以加在前面那種日誌記錄方式中。

如何讓日誌滾動起來

滾動日誌,顧名思義,記錄一個模組的日誌用一系列的記錄檔,同一模組檔案個數有限制,最多maxNum個,大小也有限制,最大maxSize位元組,檔案名稱有一定的命名方式,比如: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

當記錄檔個數到達限制maxNum的時候就會啟動淘汰機制,刪除最老的日誌,比如說maxNum設定為10,這個時候算上testlog.log一共最多有10個檔案,當滾動的時候如果存在testlog_9.log就會從testlog_8.log開始滾動,覆蓋掉testlog_9.log,這樣就可以保證日誌正常記錄,而且不會出現很大很大的記錄檔,保證日誌系統的正常運行。

代碼實現

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

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 最大記錄檔數目

* @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;

//擷取最大的滾動日誌數目

for(;$index < $this->maxFileNum; $index++)

{

if(!file_exists($this->logPath.DIRECTORY_SEPARATOR.$this->file."_".$index.".log"))

{

break;

}

}

//已經存在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 記錄調試資訊

* @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");

}

}

}

看個例子

#例子中設定記錄層級為msg(此時debug資訊是不會紀錄的),記錄檔個數為5,大小為200個位元組(測試方便),檔案名稱為testlog

1

2

3

4

5

$logs = LOGS::getInstance();

$logs->init(1, 5, 200, "./", 'testlog');

$logs->msg("YRT", __FILE__, __LINE__);

$logs->debug("YRT", __FILE__, __LINE__);

當我們不停的運行這個例子的時候,會在代碼所在檔案夾下產生5個檔案就像下面這樣:

1

2

3

4

5

testlog_4.log

testlog_3.log

testlog_2.log

testlog_1.log

testlog.log #最新的日誌在這個檔案中

http://www.bkjia.com/PHPjc/1014276.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1014276.htmlTechArticlePHP滾動日誌的代碼實現 PHP滾動日誌類庫 PHP記錄日誌,我之前接觸過的有按照年月分檔案夾,然後按照日分檔案的日誌記錄方式,這種方式...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.