PHP scrolling Log Code Implementation _php Tutorial

Source: Internet
Author: User

Code Implementation of PHP scrolling log


PHP Scrolling Log Class Library

PHP logging, I have been contacted before the date of the folder, and then according to the log records of the day, this way, there are pros and cons, there is his use of the scene, I would like to say today is another way of logging-file scrolling way to log, of course, This kind of scrolling mechanism can also be added to the previous type of logging method.

How to make the log roll up

Scroll log, as the name implies, record a module of the log with a series of log files, the number of the same module is limited, up to Maxnum, the size of the limit, the maximum maxsize bytes, filenames have certain naming methods, such as: Testlog.log, Testlog_1.log , Testlog_2.log 、、、、、、 where Testlog.log is the log file being used, and when the Testlog.log file size reaches the limit maxsize the log file is scrolled back, as follows:

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 reached the limit maxnum will start the elimination mechanism, delete the oldest logs, such as Maxnum set to 10, this time count Testlog.log altogether up to 10 files, when scrolling if there is testlog_9. Log will start rolling from the Testlog_8.log, overwriting the testlog_9.log, so that the log can be guaranteed to record, and there will not be very 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

21st

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;

The 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:: $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 save path $logPath string log file

* @Param $file string log file name prefix

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

*/

Public Function Formattime ()

{

$ustime = Explode ("", Microtime ());

Return "[". Date (' y-m-d h:i:s ', Time ()). ".". ($ustime [0] * 1000). "]";

}

/**

* Log files are recorded @Desc scrolling way

*/

Public function log ($STR)

{

$path = $this->logpath.directory_separator. $this->file. ". Log ";

Clearstatcache ();

if (file_exists ($path))

{

if (FileSize ($path) >= $this->maxfilesize)

{

$index = 1;

Get the maximum number of scroll logs

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

{

if (!file_exists ($this->logpath.directory_separator. $this.->file. " _ ". $index.". Log "))

{

Break

}

}

There are already maxfilenum log files.

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 the file where the string log is located

* @Param string log is in the row

*/

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 the file where the string log is located

* @Param string log is in the row

*/

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

{

if ($this->level <= self::logs_msg)

{

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

}

}

/**

* @Desc Log error messages

* @Param string log information

* @Param the file where the string log is located

* @Param string log is in the row

*/

Public Function Err ($msg, $file, $line)

{

if ($this->level <= self::logs_err)

{

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

}

}

}

Look at an example.

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

1

2

3

4

5

$logs = Logs::getinstance ();

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

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

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

When we run this example all the time, we generate 5 files in the folder where the code is located, as follows:

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.html www.bkjia.com true http://www.bkjia.com/PHPjc/1014276.html techarticle PHP Rolling Log code implementation of PHP rolling log class library PHP log, I have been exposed to a folder according to the date, and then according to the log record of the daily sub-file, this way ...

  • 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.