-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogRotate.php
139 lines (117 loc) · 3.82 KB
/
logRotate.php
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
<?php
namespace Primo;
class LogRotate
{
private $maxFileNum; //Maximum number of log files
private $maxFileSize;//Maximum size in MB of log files
private $maxDays;//Maximum saved days
private $logPath;//Save directory of log files
private $logFile;//Log file name without suffix. The default suffix is ".log"
private static $instance = null;
private function __construct()
{
}
private function __clone()
{
}
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* init
* @Param $maxNum int
* @Param $maxSize int
* @Param $logPath string
* @Param $file string
* @Param $maxDays int
* @Return object
*/
public function init($maxNum, $maxSize, $logPath, $file, $maxDays = 30)
{
$maxNum = intval($maxNum);
$maxSize = intval($maxSize);
$maxDays = intval($maxDays);
!is_dir($logPath) && mkdir($logPath, 0777, true);
if ($maxNum <= 0 || $maxSize <= 0 || $maxDays <= 0 || !is_dir($logPath)) {
return false;
}
$this->maxFileNum = $maxNum;
$this->maxFileSize = $maxSize * 1000 * 1000;
$this->logPath = $logPath;
$this->logFile = $file;
$this->maxDays = $maxDays;
return $this;
}
/**
* Format time by microtime()
*/
public function formatTime()
{
$ustime = explode(" ", microtime());
return "[" . date('Y-m-d H:i:s', time()) . "." . ($ustime[0] * 1000) . "]";
}
/**
* Record log files by rotation
*/
public function rotate($msg)
{
clearstatcache();
$this->clear();
$path = $this->logPath . DIRECTORY_SEPARATOR . $this->logFile . ".log";
if (file_exists($path)) {
if (filesize($path) >= $this->maxFileSize) {
$index = 1;
//Get the maximum number of rotation logs
for (; $index < $this->maxFileNum; $index++) {
if (!file_exists($this->logPath . DIRECTORY_SEPARATOR . $this->logFile . "_" . $index . ".log")) {
break;
}
}
//Maxfilenum log files already exist
if ($index == $this->maxFileNum) {
$index--;
}
//Rotate log
for (; $index > 1; $index--) {
$new = $this->logPath . DIRECTORY_SEPARATOR . $this->logFile . "_" . $index . ".log";
$old = $this->logPath . DIRECTORY_SEPARATOR . $this->logFile . "_" . ($index - 1) . ".log";
rename($old, $new);
}
$newFile = $this->logPath . DIRECTORY_SEPARATOR . $this->logFile . "_1.log";
rename($path, $newFile);
}
}
$fp = fopen($path, "a+b");
fwrite($fp, $msg, strlen($msg));
fclose($fp);
return true;
}
/**
* Clear expired log files
*/
public function clear()
{
$pattern = $this->logPath . DIRECTORY_SEPARATOR . "*.log";
foreach (glob($pattern) as $fn) {
if (filectime($fn) < strtotime('-' . $this->maxDays . ' days')) {
unlink($fn);
}
}
}
public function debug($msg)
{
$this->rotate($this->formatTime() . "[DEBUG]: ${msg}" . PHP_EOL);
}
public function info($msg)
{
$this->rotate($this->formatTime() . "[INFO]: ${msg}" . PHP_EOL);
}
public function error($msg)
{
$this->rotate($this->formatTime() . "[ERROR]: ${msg}" . PHP_EOL);
}
}