PHP rolling log code implementation _ PHP Tutorial

Source: Internet
Author: User
PHP code implementation for rolling logs. PHP rolling log code implements the PHP rolling log class library PHP to record the log. I have previously contacted folders by year and month, and then recorded by day and file, code implementation of PHP rolling logs in this way

PHP rolling log class library

PHP records logs. I have previously used folders by year and month, and recorded logs by day and file. this method has advantages and disadvantages and has application scenarios, what I want to talk about today is another log record method-file rolling method to record logs. of course, this rolling mechanism can also be added to the previous logging method.

How to scroll up logs

Rolling logs, as the name implies, use a series of log files to record the logs of a module. the number of files in a module is limited. The maximum number of files is maxNum, and the size is also limited. The maximum value is maxSize, the file name can be named in a certain way, such as testlog. log, testlog_1.log, testlog_2.log, and testlog. log is a log file in use, when testlog. when the log file size reaches the limit of maxSize, it will scroll back to the log file, as shown below:

The code is 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 reaches the limit of maxNum, the elimination mechanism is enabled to delete the oldest logs. for example, if maxNum is set to 10, testlog is counted. log has a total of 10 files. if testlog_9.log exists during scrolling, it will start to scroll from testlog_8.log and overwrite testlog_9.log. This ensures normal log records, there will be no large log files to ensure the normal operation of the log system.

Code implementation

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;

// Log level 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 initialization

* @ 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 storage path

* @ Param $ file string the prefix of the log file name

* @ 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 get the formatted time string

*/

Public function formatTime ()

{

$ Ustime = explode ("", microtime ());

Return "[". date ('Y-m-d H: I: S', time ()). ". ". ($ ustime [0] * 1000 ). "]";

}

/**

* @ Desc record log files in scroll mode

*/

Public function log ($ str)

{

$ Path = $ this-> logPath. DIRECTORY_SEPARATOR. $ this-> file. ". log ";

Clearstatcache ();

If (file_exists ($ path ))

{

If (filesize ($ path) >=$ this-> maxFileSize)

{

$ Index = 1;

// Obtain the maximum number of rolling logs

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

{

If (! File_exists ($ this-> logPath. DIRECTORY_SEPARATOR. $ this-> file. "_". $ index. ". log "))

{

Break;

}

}

// MaxFileNum log files already exist

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 record debugging information

* @ Param string log information

* @ Param string the file where the log is located

* @ Param string the row of the log

*/

Public function debug ($ msg, $ 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 the file where the log is located

* @ Param string the row of the log

*/

Public function msg ($ msg, $ file, $ line)

{

If ($ this-> level <= self: LOGS_MSG)

{

$ This-> log ($ this-> formatTime (). "[{$ file }:{ $ line}] MSG :$ {msg} \ n ");

}

}

/**

* @ Desc record the error message

* @ Param string log information

* @ Param string the file where the log is located

* @ Param string the row of the log

*/

Public function err ($ msg, $ file, $ line)

{

If ($ this-> level <= self: LOGS_ERR)

{

$ This-> log ($ this-> formatTime (). "[{$ file }:{ $ line}] ERR :$ {msg} \ n ");

}

}

}

Let's look at an example.

# In the example, set the record level to msg (the debug information will not be recorded at this time), the number of log files is 5, and the size is 200 bytes (for Test convenience). the file name is 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 __);

When we keep running this example, five files will be generated in the folder where the code is located, as shown below:

1

2

3

4

5

Testlog_4.log

Testlog_3.log

Testlog_2.log

Testlog_1.log

Testlog. log # The latest log is in this file

The rolling PHP log class library PHP records logs. I used to share folders by year and month, and then recorded logs by day and file. this method...

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.